• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing non-static from static context error

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic