16

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 ?

Patryk
  • 22,602
  • 44
  • 128
  • 244
  • 3
    It's worth mentioning that GenTest is not a subclass of JavaApp1. It is a nested class. The difference is that subclasses inherit the public and protected properties of their parent class. – ccoakley Apr 24 '12 at 16:23
  • Just put GenTest in its own file. There's no need to nest it in JavaApp1. – Hovercraft Full Of Eels Apr 24 '12 at 16:23
  • 1
    http://stackoverflow.com/questions/5272957/problem-creating-object-of-inner-class-in-java –  Apr 24 '12 at 16:27

8 Answers8

21

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

It should be static class GenTest, as you create an instance of it from static method.

Also, in general, nested classes should be static.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MByD
  • 135,866
  • 28
  • 264
  • 277
1

The class GenTest is a non-static class and therefore must be instantiated within an instance of JavaApp1. If you do static class GenTest what you have work otherwise you need to create an instance of JavaApp1 and create the GenTest within a non-static method.

John B
  • 32,493
  • 6
  • 77
  • 98
1

Thar's because GenTest is defined withing the context of JavaApp1. So you can refer to it unless you have an instance of JavaApp1. Change it to a static class for it to work.

static class GenTest...

mprivat
  • 21,582
  • 4
  • 54
  • 64
1

Please Use

static class GenTest()......
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
0

The way you are invoking isn't the correct way to do that. Since the inner class GenTest is a member of the JavaApp1 the correct way to invoke it would be

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

Using it your class would compile correctly.

Patryk
  • 22,602
  • 44
  • 128
  • 244
0

The class is nested which means that your nested class is not static, which means you have to create an object for the nested class through the object of the main class. what that means is your psvm should be like this.

public static void main(String[] args) {
    JavaApp1 a=new JavaApp1(); //create an object for the main class
    JavaApp1.GenTest x=a.new GenTest();

    x.oldFunction();
    x.newFunction();
}
0
class Test {

    static class GenTest { // nested class with static

        static void oldFunction() { // static method
            System.out.println("don't use that");
        }
        void newFunction() { // non-static method
            System.out.println("That's ok.");
        }
    }

    public static void main (String[] args) {
        GenTest.oldFunction(); // call static method

        GenTest two = new GenTest(); // call non-static method
        two.newFunction();
    }

}

Java Online java

antelove
  • 3,216
  • 26
  • 20