15 Instance inner class

An instance inner class refers to an inner class that is not decorated with static, and is also called a non-static inner class in some places. The sample code is as follows:

public class Outer {
    class Inner {
        // instance inner class
    }
}

The Inner class in the above example is the instance inner class. The instance inner class has the following characteristics.

1) In the static method of the outer class and other classes other than the outer class, the instance of the inner class must be created through the instance of the outer class.

public class Outer {
    class Inner1 {
    }
    Inner1 i = new Inner1(); // No need to create external class instance
    public void method1() {
        Inner1 i = new Inner1(); // No need to create external class instance
    }
    public static void method2() {
        Inner1 i = new Outer().new inner1(); // Need to create an instance of the outer class
    }
    class Inner2 {
        Inner1 i = new Inner1(); // No need to create external class instance
    }
}
class OtherClass {
    Outer.Inner i = new Outer().new Inner(); // Need to create an instance of the outer class
}

2) In the instance inner class, all members of the outer class can be accessed.

public class Outer {
    public int a = 100;
    static int b = 100;
    final int c = 100;
    private int d = 100;
    public String method1() {
        return "instance method 1";
    }
    public static String method2() {
        return "static method 2";
    }
    class Inner {
        int a2 = a + 1; // access public a
        int b2 = b + 1; // access static b
        int c2 = c + 1; // access final c
        int d2 = d + 1; // access private d
        String str1 = method1(); // access instance method method1
        String str2 = method2(); // access static method method2
    }
    public static void main(String[] args) {
        Inner i = new Outer().new Inner(); // Create inner class instance
        System.out.println(i.a2); // output 101
        System.out.println(i.b2); // output 101
        System.out.println(i.c2); // output 101
        System.out.println(i.d2); // output 101
        System.out.println(i.str1); // output instance method 1
        System.out.println(i.str2); // output static method 2
    }
}

Tip: If there are multiple levels of nesting, the inner class can access all members of the outer class.

3) The members of the inner class cannot be accessed directly in the outer class, but must be accessed through an instance of the inner class. If class A contains inner class B, and class B contains inner class C, then class C cannot be accessed directly in class A, but class C should be accessed through an instance of class B.

4) There is a one-to-many relationship between an instance of an outer class and an instance of an inner class, that is to say, an instance of an inner class corresponds to only one instance of an outer class, and an instance of an outer class can correspond to multiple instances of an inner class.

If the instance inner class B and outer class A contain a member t with the same name, then both t and this.t in class B represent member t in B, and A.this.t represents member t in A.

public class Outer {
    int a = 10;
    class Inner {
        int a = 20;
        int b1 = a;
        int b2 = this.a;
        int b3 = Outer.this.a;
    }
    public static void main(String[] args) {
        Inner i = new Outer().new Inner();
        System.out.println(i.b1); // output 20
        System.out.println(i.b2); // output 20
        System.out.println(i.b3); // output 10
    }
}

5) Static members cannot be defined in an instance inner class unless final and static are used at the same time.

Posted by vanlier on Fri, 13 May 2022 19:21:40 +0300