Results 1 to 11 of 11
  1. #1
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default Cannot make a static reference to the non-static method

    Java Code:
    import java.util.Scanner;
    
    public class Exercise7_1M {
    	
    	class Rectangle {
    	// The width and height of the rectangle
    	double width = 1;
    	double height = 1;
    	
    	//Construct a rectangle object
    	Rectangle() {
    	}
    	
    	//Construct a rectangle object
    	Rectangle(double newWidth, double newHeight){
    		width = newWidth;
    		height = newHeight;
    	}
    	
    	//Return the area
    	double getArea() {
    		return width * height;
    	}
    	
    	//Return the width
    	double getWidth()
    	{
    		return width;
    	}
    	
    	//Return the width
    	double getHeight()
    	{
    		return height;
    	}
    	
    	//Return the Perimeter 
    	double getPerimeter()
    	{
    		return width * 2 + height * 2;
    	}
    	
    }
    
    
    	public void main(String[] args)
     	{
    		Scanner input = new Scanner(System.in);
    		
    		 double width = input.nextDouble();
    		 double height = input.nextDouble();
    		
    		 Rectangle rectangle1 = new Rectangle();
    		 System.out.println("The area of a rectangle with width " + Rectangle.getWidth() + "and height " + Rectangle.getHeight() + "is " + Rectangle.getArea()
    		 + "The perimeter is " + Rectangle.getPerimeter() + "with interior color ");
     	}
    }

    I took a couple c++ classes and then signed up for Java this semester, I got about a month and a half ahead in the first couple weeks as it was all very simple stuff as an intro class, stopped going to class because it wasn't required and figured I'd pick back up once it got into new stuff for me. I was waiting a few weeks past where i was before I went back and got seriously ill and ended up being over 6 weeks behind in assignments. I'm catching up now and am hung up on this assignment.
    I can normally get the logic down, but Ive been having problems with rules of how things work. I wrote a program to take 10 numbers in and calculate the standard deviation and mean in about 10 minutes to spend about an hour and a half figuring out why my answer was inaccurate after the 8th decimal point. I was using float instead of double. For whatever reason i had it in my head that double was what a long was, and there wasn't something bigger for float.
    Now I'm having this compiler error problem due to my lack of understanding of what it means.
    Cannot make a static reference to the non-static method
    Can someone point me to a good guide or explain this? I can't seem to figure out a way to get around this and have probably made my code messy trying to do so.

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    You are using the methods of Rectangle as if they were static. They are not.

    Have a look at Understanding Instance and Class Members.
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Now I'm having this compiler error problem due to my lack of understanding of what it means.
    Cannot make a static reference to the non-static method

    It's a good idea to copy and post the entire compiler message as well as indicating which line of your code it is referring to.

    Can someone point me to a good guide or explain this?
    Oracle's Tutorial discusses this in the section on Understanding Instance and Class Members. Given that you have missed on things like this and data types you might want to read through the Class and Objects section of which it is part. And also Langauge Basics.

    Bottom line, though: you use expressions like "Rectangle.getWidth()" and the compiler is saying "Which rectangle?". Only specific rectangles have a width, not rectangles-in-general. So you can't say "Rectangle.getWidth()" only "someRectangleOrOther.getWidth()".

  4. #4
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    Thanks for the help, I don't think I have as good as understanding as I'd like for all of this, but I do believe I fixed that error after looking at Understanding Instance and Class Members.
    Now I'm having another issue

    This is what my code looks like now, and the error I'm getting.
    Java Code:
    import java.util.Scanner;
    
    public class Exercise7_1M
    {
    	
    	public static void main(String[] args)
     	{
    		System.out.println("before scanner");
    		Scanner input = new Scanner(System.in);
    		
    		 double width = input.nextDouble();
    		 double height = input.nextDouble();
    		
    		 Rectangle rectangle1 = new Rectangle(width, height);
    		 System.out.println("The area of a rectangle with width " + rectangle1.getWidth() + "and height " + rectangle1.getHeight() + "is " + rectangle1.getArea()
    		 + "The perimeter is " + rectangle1.getPerimeter() + "with interior color ");
     	}
    	
    	class Rectangle 
    	{
    		// The width and height of the rectangle
    		double width = 1;
    		double height = 1;
    		
    		//Construct a rectangle object
    		Rectangle() {
    		}
    		
    		//Construct a rectangle object
    		Rectangle(double newWidth, double newHeight){
    			width = newWidth;
    			height = newHeight;
    		}
    		
    		//Return the area
    		double getArea() {
    			return width * height;
    		}
    		
    		//Return the width
    		double getWidth()
    		{
    			return width;
    		}
    		
    		//Return the width
    		double getHeight()
    		{
    			return height;
    		}
    		
    		//Return the Perimeter 
    		double getPerimeter()
    		{
    			return width * 2 + height * 2;
    		}
    	
    	}
    	
    	
    
    }
    & the error:
    Java Code:
    Line 14: No enclosing instance of type Exercise7_1M is accessible. Must qualify the allocation with an enclosing instance of type Exercise7_1M (e.g. x.new A() where x is an instance of Exercise7_1M).

    I'm not sure why I'm feeling so much more overwhelmed with java than I ever did with C++, I guess because I was thrown into it deeper than a more gradual step by step with C++ even though I always felt it was too easy. I'm going to blame it on the meds I'm still on even though I'm pretty sure they have nothing to do with it :p


    Thanks again for the help, guys. =)

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I am fairly new to Java and this issue reaches the borders of my knowledge... I think the error has to do with the fact that the Rectangle is an internal class (it resides within you Exercise class. If you take it out of there the code works just fine.

    @javaguru: why does an internal class like this produce the aforementioned error?
    I'm new to Java but I like to help where ever I can. :)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    OK - this message is a bit obscure as it involves outre things lke inner classes. You don't need to know about them at this stage!

    What you do need to do is put all the "class Rectangle { ... }" either into its own file (Rectangle.java) and make it public class Rectangle or move the Reactangle class code outside the Excercise7_1M class.

    The latter case would look like:

    Java Code:
    import java.util.Scanner;
    
    public class Exercise7_1M
    {
        public static void main(String[] args)
        {
            // etc
        }
    }
    
    class Rectangle 
    {
        // etc
    }
    -----------------

    Java was designed with C++ in mind: particularly C++'s complexity. It really is a simpler language.

    But that's little comfort to you at this stage. In making it simpler, they made it different: and you just have to cope with that. I'm sure you will.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Erik:

    Java Code:
    Rectangle rectangle1 = new Rectangle(width, height);

    So the compiler says, "OK, he wants me to make a Rectangle. But what sort of Rectangle?!?!?". The point is that because Rectangle is an inner class of Excercise7_1M, every instance of the excercise class defines within it a Rectangle type. A particular instance of Excercise7_1M is required to act as the "context" of the Rectangle being created. (Example here of an iterator inner class: a particular instance of the data structure acts as the context of the iterator.)

    So the following would "work" (OP: don't do this)

    Java Code:
    Excercise7_1M context = new Excercise7_1M();
    Rectangle rectangle1 = context.new Rectangle(width, height);

    Or you could make Rectangle a static nested class... (See the page after the one I linked to)
    Last edited by pbrockway2; 11-14-2010 at 12:39 AM.

  8. #8
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    Thanks! I was going to put it in another file to start with, but realized it wouldn't work to turn in. We turn our assignments in by coping and pasting the code into our teachers website after logging in and it complies and runs it with his sample input and checks character for character the output. So I figured it would all need to be in one, I knew the Exercise7_1M had to be exactly that or it won't compile on his site so I figured it would all need to be under that and didn't think about it again.

    I separated it out as you suggested and it works fine in both Eclipse and for turning it in online.

    Thank you so much for the help. Now onto the next assignment, hopefully I won't run into so many issues on that one, but I may be back. =)

  9. #9
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    Okay, I got my next assignment done with little trouble. Just had to take in two strings and see if they are an anagram or not. Only hold up was it failed one of the five tests because in cases like hello and helo it would say it was an anagram. Just put a check at the start to compare if both strings were the same length or not and program worked great.

    The one I'm having trouble with now we just have to take in an SS number and check if it follows the format of
    ###-##-#### or not.
    I was just going to put it into a char array and check if the numbers were 0-9 and the -s were -s but they teacher wants us to use a regular expression to do it.
    If I understand correctly, I'm going to want a Pattern, Matching, and Boolean but I'm a little confused on how to tell it to match for a pattern of 3 digits - 2 digits - 4 digits.
    My java book has a short section on this which shows an example of using .matches to check if a specific word is in a string but that's not really the same as what I am trying to do here. When I try to search online for examples using matches I cant seem to find anything that is checking for a format in a string. Any help?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Don't mix topics. This is a new question and is unrelated to the earlier discussion, so it merits its own thread.

    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    db

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  2. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  3. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 04:17 AM
  4. make static ref to non-static method?
    By McChill in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 06:48 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •