Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "non-static variable this cannot be referenced from a static context"?

I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent's class I get :

non-static variable this cannot be referenced from a static context

On the other hand when I put the sublass GenTest's class code outside the the "parent's" class code - JavaApp1 I do not get this error.

public class JavaApp1 {

    class GenTest {  
        @Deprecated
        void oldFunction() {
            System.out.println("don't use that");
        }
        void newFunction() {
            System.out.println("That's ok.");
        }
    }

    public static void main(String[] args) {
        GenTest x = new GenTest();
        x.oldFunction();
        x.newFunction();
    }
}

Why is this happening ?

like image 859
Patryk Avatar asked Apr 24 '12 16:04

Patryk


People also ask

How do you resolve non-static variable this Cannot be referenced from a static context?

Therefore, this issue can be solved by addressing the variables with the object names. In short, we always need to create an object in order to refer to a non-static variable from a static context. Whenever a new instance is created, a new copy of all the non-static variables and methods are created.

How do you access a non-static variable from a static context?

The only way to access a non-static variable from a static method is by creating an object of the class the variable belongs. This confusion is the main reason why you see this question on core Java interview as well as on core Java certifications e.g. OCAJP and OCPJP exam.

Why non-static method Cannot be referenced from a static context?

A non-static method is dependent on the object. It is recognized by the program once the object is created. But a static method can be called before the object creation. Hence you cannot make the reference.

What do Cannot be referenced from a static context?

For the same reasons, a non-static method cannot be referenced from a static context, either, as the compiler cannot tell which particular object the non-static member belongs to.


1 Answers

Your nested class (which isn't a subclass, by the way) isn't marked as being static, therefore it's an inner class which requires an instance of the encoding class (JavaApp1) in order to construct it.

Options:

  • Make the nested class static
  • Make it not an inner class (i.e. not within JavaApp1 at all)
  • Create an instance of JavaApp1 as the "enclosing instance":

    GenTest x = new JavaApp1().new GenTest();
    

Personally I'd go with the second approach - nested classes in Java have a few oddities around them, so I'd use top-level classes unless you have a good reason to make it nested. (The final option is particularly messy, IMO.)

See section 8.1.3 of the JLS for more information about inner classes.

like image 78
Jon Skeet Avatar answered Nov 16 '22 02:11

Jon Skeet