wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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

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

Joined: Jul 08, 2003
Posts: 24183

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]
It is sorta covered in the JavaRanch Style Guide.
subject: accessing non-static from static context error
Similar Threads
About Overriden
Unclear Concepts
override or overload?
ClassCastException ??
protected constructors