This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Cannot reference static method?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Evildoers! Eat my justice! And this tiny ad's justice too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic