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).
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With