aspose file tools*
The moose likes Beginning Java and the fly likes Static method call from non static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static method call from non static method" Watch "Static method call from non static method" New topic
Author

Static method call from non static method

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
It is my understanding that static methods can't access non-static methods or non-static instance variables, however I wanted to be sure I'm clear in the opposite case, specifically calling a static method from within a non static method. Also, I'm pretty sure static variables may be incremented from a non-static method, ie constructor(). So the question I think really has to do with calling a static method from within a non static method:
class Avg2 extends Thingee
{
static private int WidgetCount = 0;
public string wName;
int wNumber;
private static synchronized in addWidget()
{
return ++Widgetcount;
}
public Avg2()
{
wNumber = addWidget()
}
}
Thanks in advance for your help
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
yuo are correct in your assumption.
no problem calling static methods or variables from non static methods.
the opposite you cant as you mentioned.
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Thanks for your insight. I believe one of the mock exams may have an error indicating that a "call to a static method from a non-static method results in a compiler error". Perhaps I could see some error occuring if wNumber were private?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method call from non static method