Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate non static inner class within a static method?

I have the following piece of code:

public class MyClass {     class Inner {      int s, e, p;    }     public static void main(String args[]) {      Inner in;    } } 

Up to this part the code is fine, but I am not able to instantiate 'in' within the main method like in = new Inner() as it is showing non static field cannot be referenced in static context.

What is the way I can do it? I do not want to make my Inner class static.

like image 275
Victor Mukherjee Avatar asked Oct 02 '12 12:10

Victor Mukherjee


People also ask

How do you instantiate a non-static inner class?

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. InnerClass innerObject = outerObject.

Can you have a static method in a non-static inner class?

A non-static nested class has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.

How do you access non-static members within a static method?

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.

How do you instantiate a static inner class?

We can instantiate a static inner class with reflection using InnerClass. class. newInstance(). If we need an instance of the outer class to instantiate a non-static inner class, we can specify it before a new operator.


2 Answers

You have to have a reference to the other outer class as well.

Inner inner = new MyClass().new Inner(); 

If Inner was static then it would be

Inner inner = new MyClass.Inner(); 
like image 180
RNJ Avatar answered Oct 06 '22 10:10

RNJ


A "regular" inner class has a hidden (implicit) pointer to a Outer class instance. This allows the compiler to generate the code to chase the pointer for you without you having to type it. For instance, if there is a variable "a" in the outer class then the code in your inner class can just do "a=0", but the compiler will generate code for "outerPointer.a=0" maintaining the hidden pointer under the covers.

This means when you create an instance of an inner class you have to have an instance of a outer class to link it to. If you do this creation inside a method of the outer class then the compiler knows to use "this" as the implicit pointer. If you want to link to some other outer instance then you use a special "new" syntax (see code snippet below).

If you make your inner class "static" then there is no hidden pointer and your inner class cannot reference members of the outer class. A static inner class is identical to a regular class, but its name is scoped inside the parent.

Here is a snippet of code that demonstrates the syntax for creating static and non-static inner classes:

public class MyClass {      int a,b,c; // Some members for MyClass      static class InnerOne {         int s,e,p;         void clearA() {             //a = 0;  Can't do this ... no outer pointer         }     }      class InnerTwo {         //MyClass parentPointer;      Hidden pointer to outer instance         void clearA() {                      a = 0;             //outerPointer.a = 0      The compiler generates this code         }            }      void myClassMember() {         // The compiler knows that "this" is the outer reference to give         // to the new "two" instance.         InnerTwo two = new InnerTwo(); //same as this.new InnerTwo()     }      public static void main(String args[]) {          MyClass outer = new MyClass();          InnerTwo x = outer.new InnerTwo(); // Have to set the hidden pointer         InnerOne y = new InnerOne(); // a "static" inner has no hidden pointer         InnerOne z = new MyClass.InnerOne(); // In other classes you have to spell out the scope      }  } 
like image 22
ChrisCantrell Avatar answered Oct 06 '22 11:10

ChrisCantrell