aspose file tools*
The moose likes Java in General and the fly likes accessing non-static from static context error 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 » Java in General
Bookmark "accessing non-static from static context error" Watch "accessing non-static from static context error" New topic
Author

accessing non-static from static context error

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
I have following code snippet in that subclass.java throws an error message that non-static getNumber() method cannot be referenced from static context.





Whats wrong in subclass.java code, I am not accessing getNumber() method from within main method which is static? Can anybody please let me know?


I am slow but sure
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The initialization of static members like "a" happens in a static method named "<clinit>", specially created by the compiler to hold such code. So you are, indeed, calling getNumber() from a static method.

Most likely your real code is more complex, but in this case, since all getNumber() does is provide access to a static member, why can't it be static?


[Jess in Action][AskingGoodQuestions]
 
 
subject: accessing non-static from static context error