"Java inner class", There were many places I didn't know that I knew unexpectedly, so let's review it.
As the word says, it is a class inside the class.
As an application, it provides better concealment (encapsulation). You can hide classes that are meaningless away from the outer class. 例:Cow.javaとCowLeg.java
Which one is relevant is very important and It turned out that the usage was decided.
It is treated the same as non-static fields, methods, constructors, etc. of external classes. ⇒ Items that cannot be used unless the instance exists.
(You can also add static, but it will be a static inner class, so later)
private,protected visibility
public class Outer {
public class PublicInner {}
private class PrivateInner {}
public class PackagePrivateInner {}
}
Visibility
public class Outer {
private int outerVal;
public class Inner {
public int innerVal;
private void method() {
// OK
this.innerVal = Outer.this.outerVal;
}
}
private void method() {
// NG
// this.outerVal = innerVal;
}
}
Non-static inner classes are associated with __outer class instances __, so
Instantiation of non-static inner class
Outer.Inner inner = (new Outer()).new Inner();
Access from external class
public class Outer {
private int outerVal;
public class Inner {
private int privateA = 1;
protected int protectedB = 2;
public int publicC = 3;
}
private void method() {
Inner inner = new Inner();
System.out.println(inner.publicC);
System.out.println(inner.protectedB);
System.out.println(inner.privateA);
}
}
Access from other classes
public static void main(String[] args) {
Outer.Inner inner = new Outer().new Inner();
System.out.println(inner.publicC);
System.out.println(inner.protectedB);
// NG
// System.out.println(inner.privateA);
}
An instance of an inner class
Why you can see Inner private from __Outer: __
Outer$Inner@461 I understand that Inner is not independent, but part of Outer. Therefore, since it is in the same class, Private of Inner can also be seen in Outer. The same was true for static inner classes.
__ I'm looking for a better interpretation! !! !! __
Compile error: 「Inner classes cannot have static declarations」
static members are NG
public class Outer2 {
public class Inner {
static { /** something **/ }
public static int staticValue;
public static void staticMethod() { /** something **/ }
}
}
Non-static inner classes are associated with __outer class instances __, so 1.Since static is for associating with a class, not an instance Compile error because it is inconsistent in terms of grammar
Variable precedence
public class Outer {
String a = "outer-a";
String b = "outer-b";
String c ="outer-c";
public class Inner {
String a = "inner-a";
String b = "inner-b";
private void method() {
String a = "method-a";
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(this.a);
System.out.println(Outer.this.a);
}
}
public static void main(String[] args) {
new Outer().new Inner().method();
}
}
method-a
inner-b
outer-c
inner-a
outer-a
The priority of non-static inner classes is
You can specify the variable range with this.variable, Outer.this.variable. You can see from this debugged figure! !!
It's just like a regular class! !! However, it also has its own characteristics as an inner class.
Like the non-static inner class, the upper level is a class, so private and protected can be used.
Because it belongs to the outer class as well as the non-static inner class.
Instantiation of static inner class
Outer.Inner inner = new Outer.Inner();
python
public static void main(String[] args) {
class Inner {
protected int a = 1;
}
class InnerSub extends Inner {
private int b = 2;
}
InnerSub sub = new InnerSub();
System.out.println(sub.a + sub.b);
}
From Effective Java 2nd Edition (The Java Series)
Recommended Posts