Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a constructor associated with nested classes

I want to know if there are any constructors involved with inner classes. for example consider the code snippet given 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 both 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 snippet compiles fine and gives output of "The value of x is 10".

What i want to know here is whether a constructor is invoked when new() is used with MyInner class and MyOuter class. If yes, then is there any constructor chaining from inner class to outer class (like subclass calls constructor of super class and so on).

like image 314
chaitu Avatar asked Apr 13 '12 05:04

chaitu


People also ask

Can nested classes have constructors?

They have access to both static and non-static members in the enclosing context. They can only define instance members. They're the only type of nested classes that cannot define constructors or extend/implement other classes or interfaces.

Can we write nested constructor in Java?

Now you can create NestedClassExtension instances in this way: NestedClassExtension extension = new NestedClassExtension(new MainClass("main"), "nested"); So the main class has to exist, and its constructor it is called first. Then the constructors of the nested classes.

How do you call an inner class constructor?

If you want to create an instance of an inner nested class you need to provide a class object of the enclosing class as an extra parameter with Class#getDeclaredConstructor. If the nested class is static you will not need this enclosing instance.

Can static inner class have constructor?

Constructors are not allowed to be static in Java because of the following reason: In Java, static methods and variables apply to the classes. But a constructor is called when a new operator is used to create an instance. Since it does not belong to the property class, it is not allowed to be static.


1 Answers

You can observe the constructor chain for the inner class when you extend an inner class.

Take this example:

public class MainClass {

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

    public class NestedClass {

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

}

and then extend the NestedClass like this

public class NestedClassExtension extends NestedClass {

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

so you can see that you are able to call the super constructor of your nested class passing to that constructor the MainClass, and calling .super on mainClass object instance.

Now you can create NestedClassExtension instances in this way:

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

So the main class has to exist, and its constructor it is called first. Then the constructors of the nested classes.

Instead if you want create a NestedClass instance outside of the MainClass you have to write:

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

Another time, the MainClass has to be created first, then the nested classes.

like image 105
dash1e Avatar answered Sep 28 '22 12:09

dash1e