https://qiita.com/hp_kj/items/dd533d98f9d065bd0301 When I searched for this, the word "enclosing instance" came up, so I looked it up.
According to Java A2Z An "instance of an outer class" for an inner class. An instance of an inner class is a special instance that is "contained within an instance of an outer class". Therefore, in order to create an instance of the inner class, an instance of the outer class is always required. The "outer class instance" is the "enclosing instance". An instance of an inner class is an "enclosing" or "enclosing" instance.
First of all, this seems to be the conclusion. And As a meaning
about it. Applying to the previous code,
public class Main {
public static void main(String []args) {
Outer o = new Outer();///1-1
Outer.Inner oi = o.new Inner();///2-1
oi.innerPrint();
}
public static class Outer{///1
int outerhp = 10;
public class Inner{///2
public void innerPrint() {
System.out.println(Outer.this.outerhp);
}
}
}
}
I am creating an instance of external class 1 (Outer) in 1-1. Then, in 2-1 I created an instance of inner class 2 (Inner) included in the instance of the outer class.
Recommended Posts