| 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: 24054
|
|
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
|
|
|