7


Here is the code.

public class Test {
        class InnerClass{

               }

   public static void main(String[] args){
            InnerClass ic = new InnerClass();
        }
    }

It says the error message

non-static variable this cannot be referenced from a static context
after creation of object ic.

Can anyone give me the reason?

Thanks

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
bunkdeath
  • 2,348
  • 5
  • 21
  • 23
  • 1
    Read here. http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static-c and http://stackoverflow.com/questions/926822/java-non-static-variable-cannot-be-referenced-from-a-static-context-error – Saurabh Gokhale Mar 11 '11 at 12:40
  • main is a static method, it can access only other static members of outer class. – user1923551 Dec 16 '13 at 10:56

3 Answers3

25

InnerClass needs to be static itself, i.e.

public class Test {

   static class InnerClass{    
   }

   public static void main(String[] args){
      InnerClass ic = new InnerClass();
   }
}

If InnerClass is not static, it can only be instantiated in the context of a parent instance of Test. The rather baroque syntax for this is:

public class Test {

   class InnerClass{    
   }

   public static void main(String[] args){
      Test test = new Test();
      InnerClass ic = test.new InnerClass();
   }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 2
    you can do it in one line too " InnerClass ic1 = new Test().new InnerClass(); " – AZ_ Sep 27 '13 at 10:32
2

Your inner class depends on an instance of the Test class. main is a static method, thus you can't create an instance of InnerClass.

I think you want to declare your inner class as static :

class Test {
    static class InnerClass { }

    public static void main(String[] args){
        InnerClass ic = new InnerClass();
    }
}

More information about nested classes : http://download.oracle.com/javase/tutorial/java/javaOO/nested.html

Short explanation

There's mainly two types of nested classes : "static nested class" and "inner class"

Static nested class are used to logically group two classes and can be used to increase encapsulation. They can be used like any other classes and, except for visibility, they don't have any particular access to the outer class fields. They can logically be instantiated in a static context.

Inner class (ie not static) are bound to a particular instance of the outer class. This means you must have an instance of the outer class to instantiate the inner class. Have a look at Skaffman second code chunk for an instantiation example. Since inner classes are bound to an instance of the outer class, they have access to every field relative to this particular instance.

I hope all this is now clearer.

krtek
  • 26,334
  • 5
  • 56
  • 84
  • Declaring InnerClass to static works, I tried it according to the error message. But still I didn't get why. Can you explain more? – bunkdeath Mar 11 '11 at 12:43
  • 1
    @bunkdeath the given link should be enough to understand the reason, but i'll update my answer a bit. @herbalessence skaffman doesn't explain anything, he just gives working code. – krtek Mar 11 '11 at 12:52
0

First, a "nested" class is static, and an "inner" class is not.

A static class is shared between all instances of the enclosing class (thus static fields are shared between all instances), while each instance has its own version of a non-static inner class.
Non-static inner classes are stored with the enclosing object (rather than class) and can only be accessed via an instance.

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164