Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: non-static method getDimensions() cannot be referenced ...

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default non-static method getDimensions() cannot be referenced ...

    Doing some deeper study on classes and objects. The object here is to get the dimensions of a rectangle, and give the area and perimeter of the rectangle. the tester is giving me a compilation error:

    RectangleTest.java:14: non-static method getDimensions() cannot be referenced from a static context
    System.out.println(Rectangle.getDimensions());

    Can somebody give me some insight?

    Thanks.

    //Jeremiah A. Walker
    //Find the area and perimeter of a rectangle
     
    public class Rectangle
    {
    	private int width = 1;
    	private int length = 1;
    	private int area;
    	private int perimeter;
    	//method to set dimensions for our rectangle;  
    	public void setDimensions(int w, int l)
    	{
    		width = w;
    		length = l;
    		area = w * l;
    		perimeter = 2 * l * w;
    	}//end method setDimensions
     
    	public String  getDimensions()
    	{
    		return String.format("%02d:%02d:%02d:%02d", width, length, area, perimeter);
    	}//end method getDimensions
     
    }//end class Rectangle

    //Jeremiah A. Walker
    //Test Rectangle class
     
    public class RectangleTest
    {
    	public static void main(String[] args)
    	{
    		//create and initialize a Rectangle object
    		Rectangle rect1 = new Rectangle();//invokes Rectangle constructor
     
    		//output string representations of the time
    		rect1.setDimensions(10,10);
    		System.out.print("The dimensions of this rectangle are: ");
    		System.out.println(Rectangle.getDimensions());
    	}//end main
    }//end class RectangleTest


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: non-static method getDimensions() cannot be referenced ...

    Rectangle is a class. You want to get the dimensions of a particular instance of Rectangle. But the way you're calling it, it looks like you're trying to get the dimensions of the entire Rectangle class, which doesn't make sense since different instances of Rectangle will have different dimensions. And you haven't declared your getDimensions() method static, hence the compiler warning.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: non-static method getDimensions() cannot be referenced ...

    I declared the method and its associated variables static, but now I get this error:
    Rectangle.java:22: unreachable statement
    return perimeter;
    ^
    1 error

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: non-static method getDimensions() cannot be referenced ...

    Quote Originally Posted by JeremiahWalker View Post
    I declared the method and its associated variables static, but now I get this error:
    Rectangle.java:22: unreachable statement
    return perimeter;
    ^
    1 error
    Why do you want to do that? As Kevin pointed out, do you wish every instance of Rectangle be the same? I'd suggest removing the static, and go back to the original code you posted. RE the error you get - take a look at how you call setDimensions (eg. rect1.setDimensions ) - this is correct syntax for calling an objects method, and the getDimensions call should be similar syntax

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: non-static method getDimensions() cannot be referenced ...

    Got it to work. Thanks.

  6. #6
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: non-static method getDimensions() cannot be referenced ...

    invoke the getDemension() method on the object rather than the class

    EDIT: Oh you solved it. nevermind lol

Similar Threads

  1. What's the difference between a static and non-static method?
    By wholegrain in forum Java Theory & Questions
    Replies: 4
    Last Post: February 23rd, 2012, 12:06 AM
  2. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 06:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM