Is there a constructor associated with nested classes

I want to know if there are any constructors related to inner classes. for example, consider the code snippet below

class MyOuter
{
   private int x= 10;

   class MyInner
   {
      void dostuff(){
         System.out.println("The value of x is "+x);
      }
   }
}

In another java file, I create instances for the MyOuter and MyInner classes, as shown below

Class Program
{
   public static void main(String [] args)
   {
      MyOuter mo = new MyOuter();
      MyOuter.MyInner mi = mo.new MyInner();
      mi.dostuff();
   }
}

The above code fragment compiles fine and gives the output "The value of x is 10".

What I want to know here is a constructor call when new () is used with the MyInner class and the MyOuter class. If so, is there any constructor chain from the inner class to the outer class (for example, the constructor of superclass class calls, etc.).

+3
source share
5 answers

.

:

public class MainClass {

    public MainClass(String value) {
        System.out.println("mainValue: " + value);
    }

    public class NestedClass {

        public NestedClass(String nestedValue) {
            System.out.println("nestedValue: " + nestedValue);
        }
    }

}

NestedClass

public class NestedClassExtension extends NestedClass {

    public NestedClassExtension(MainClass mainClass, String nestedValue) {
        mainClass.super(nestedValue);
    }
}

, MainClass .super on MainClass.

NestedClassExtension :

NestedClassExtension extension = new NestedClassExtension(new MainClass("main"), "nested");

, , . .

, NestedClass MainClass, :

MainClass mc = new MainClass("main");
mc.new NestedClass("nested");

MainClass, .

+8

MyOuter.MyInner mi = mo.new MyInner(); 

, , , .

+3

, , . - , , MyInner (int i) , ( ). ( - ) .

+2

no-arg. super() . .

.

+2

Java- Java

javap MyOuter$MyInner

, :

public class MyOuter$MyInner extends java.lang.Object{
    final MyOuter this$0;
    public MyOuter$MyInner(MyOuter);
    void dostuff();
}

, , , -. , Inner.

MyOuter.MyInner mi = mo.new MyInner(), , .

This is done automatically by the compiler, and therefore you cannot link the creation of your inner class with the creation of the outer class simply because the outer instance must already exist by the time the inner is created.

You can, however, create a chain of constructors between other declared constructors of your inner class.

For example, if such a code looks like this:

public class MyOuter
{
   private int x= 10;

   public class MyInner
   {
        private int y = 0;

        public MyInner(){
            this(10);
        }

        public MyInner(int value){
            this.y = value;
        }

        void doStuff(){
            System.out.println("The value of x is "+x);
        }
   }
}

Here I am linking constructors to an inner class.

Again, the decompiler provides an interpretation of all this to make sure that the external instance is passed as a parameter to the internal:

public class MyOuter$MyInner extends java.lang.Object{
    final MyOuter this$0;
    public MyOuter$MyInner(MyOuter);
    public MyOuter$MyInner(MyOuter, int);
    void doStuff();
}
+1
source

All Articles