Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem creating object of inner class in java


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

like image 494
bunkdeath Avatar asked Mar 11 '11 12:03

bunkdeath


People also ask

Can we create object of inner class in Java?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

What are the disadvantages of using inner classes in Java?

Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code.

Can we create object of a class inside the same class?

No, the main method only runs once when you run your program. It will not be executed again. So, the object will be created only once.

How do you create an inner class in Java?

Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.


2 Answers

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();
   }
}
like image 180
skaffman Avatar answered Oct 10 '22 03:10

skaffman


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.

like image 23
krtek Avatar answered Oct 10 '22 04:10

krtek