File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Cannot reference static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot reference static method?" Watch "Cannot reference static method?" New topic
Author

Cannot reference static method?

Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Can someone tell me what it is that I am overlooking here? I am trying o make a subclass that will use the getSurfaceArea() method that I have created. Then I will override it with a cube but for now I can't even do this. I keep getting these errors.
non-static method setHeight(int) cannot be referenced from a static context
Here is my code:
public class Square
{
int width;
int height;
int surface;
public void setWidth(int w)
{
width = w;
}
public int getWidth()
{
return width;
}
public void setHeight(int h)
{
height = h;
}
public int getHeight()
{
return height;
}
public void getSurfaceArea()
{
surface = getHeight() * getWidth();
}
}
/***********here comes the problem***************************/
public class SquareSurface extends Square
{
public void main(String[] args)
{
Square.setHeight(10);
Square.setWidth(5);
Square.getSurfaceArea();
System.out.println("The surface of this square is " + surface);
}
}
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
hi mike...
ur problem is that u r calling the methods directly with the class name. this is allowed only in the case of Static methods as static methods don't belong to any object.
so, to rectify ur problem u can do either of the two things..
1. make the methods static as

2. the another way is to make an object of the base class and then call the method as u r calling the methods from main which is a static method. try the following


i hope i have answered your question.. if still in doubt please revert back.
Thanks
Amit


****************************<br />In 24 hrs Earth rotates once on its Axis.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Mike
I guess your main method is static. This error is there because you can not access a non static method from a static context. Try making a Square class object and then invoke the method.
Eg. Square sqr = new Square();
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
As Amit said you can also make the methods static but also make the variables like int height etc. static as well. As you can not access non static vars from static methods.
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Thanks guys, I am new but not that new! I should have seen that! I need to create the object first like Square mySquare = new Square. That's why I like this forum, no matter how lame of a question I post nobody flames me.
Thanks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot reference static method?