Detailed explanation of Java internal classes

Posted by mortona on Thu, 09 Dec 2021 03:33:37 +0100

Inner class

Definition of inner class

Defining a class in another given class or method is called an inner class.
Internal classes can be divided into four types: member internal classes, local internal classes, anonymous internal classes and static internal classes. Let's introduce these four internal classes one by one.

Member inner class

It is defined in another class. The general definition format is as follows

class C{
    class D{

    }
}

Because class C is outside relative to class D, we also call class c an external class.
The inner class of a member can unconditionally access the properties and methods of the outer class, but when the outer class wants to access the properties or methods of the inner class, it must create an inner class object, and then access the properties or methods of the inner class through the object

The inner class of a member unconditionally accesses the properties and methods of the outer class

class C{
    private String name = "External class";
    public void run(){
        System.out.println("External class run");
    }
    class D{
        public void say(){
            System.out.println(name);
            run();
        }
    }
}

External classes access internal class properties and methods

class C{
    private String name = "External class";
    public void run(){
        System.out.println("External class run");
    }
    /*Use the properties and methods of the inner class*/
    public void eat(){
        D d = new D();
        System.out.println(d.value);
        d.say();
    }
    class D{
        private String value = "DDD";
        public void say(){
            System.out.println(name);
            run();
        }
    }
}

Hide external class properties or methods

If the attribute or method of the member's internal class has the same name as the external class, these attributes and methods of the external class will be hidden in the internal class. You can also call the external class. This. Attribute / method in this format.

class C{
    private String name = "External class";
    public void run(){
        System.out.println("External class run");
    }
    /*Use the properties and methods of the inner class*/
    public void eat(){
        D d = new D();
        System.out.println(d.value);
        d.say();
    }
    class D{
        private String value = "DDD";
        private String name = "Inner class";
        public void say(){
            System.out.println(C.this.name);
            System.out.println(name);
            run();
        }
    }
}

Create an internal class object

Obviously, the internal class of a member is parasitic on the external class. To create an internal class object, you must first create an external class object. There are two ways to create inner classes later.

public class Test10 {
    public static void main(String[] args) {
        /*Method 1: create a member inner class object*/
        C c = new C();
        C.D d = c.new D();
        /*Method 2: create a member inner class object*/
        C.D d1 = c.getClassD();
    }
}
class C{
    private String name = "External class";
    public void run(){
        System.out.println("External class run");
    }
    /*Create a method that returns a D object*/
    public D getClassD(){
        return new D();
    }
    /*Use the properties and methods of the inner class*/
    public void eat(){
        D d = new D();
        System.out.println(d.value);
        d.say();
    }
    class D{
        private String value = "DDD";
        private String name = "Inner class";
        public void say(){
            System.out.println(C.this.name);
            System.out.println(name);
            run();
        }
    }
}

Access to classes inside members

Four access modifiers can be added before the inner class of a member.
private: only external classes are accessible.
protected: accessible under the same package or inherited classes.
default: accessible under the same package.
public: all classes are accessible.

Local inner class

Local inner classes exist in methods.
The difference between it and the member inner class is that the access permission of the local inner class is limited to the method or scope.

class K{
    public void say(){
        class J{
            
        }
    }
}

Note: local inner classes are like local variables. Modifiers and static modifiers cannot be accessed in front of them.

Anonymous Inner Class

Let's take a preliminary look at the anonymous inner class through a piece of code.

public class Test13 {
    public static void main(String[] args) {
        driveCar(new Car(){
            @Override
            public void drive() {
                System.out.println("Driving BMW automobile");
            }
        });
    }
    public static void driveCar(Car car){
        car.drive();
    }
}

interface Car {
    void drive();
}

After analyzing the above code, we know that the static method driveCar needs a Car object. We create an anonymous class object through the implementation interface and pass it to the past. In fact, you can also create an anonymous inner class object by inheriting a class.
Note: anonymous inner classes have no constructor. It is also the only inner class without a constructor. Anonymous inner classes and local inner classes can only access the final variable of the external class.

Static inner class

Static inner class has one more static modifier than member inner class. It is similar to the static member variable of a class and does not depend on the external class. At the same time, the static inner class also has its particularity. Because only static fields will be loaded when external classes are loaded, static internal classes cannot use non static variables and methods of external classes.
At the same time, you can know that the inner class of a member cannot contain static properties or methods.

class U {
    static class I {
        
    }
}

Benefits of inner classes

  1. The Java multi inheritance mechanism is improved. Since each internal class can inherit an interface or class independently, whether the external class inherits or implements a class or interface has no impact on the internal class.
  2. Easy to write event drivers.

summary

public class Test15 {
    public static void main(String[] args) {
        //Initialize bean1
        Test15.Bean1 bean1 = new Test15().new Bean1();
        bean1.i++;
        //Initialize bean2
        Test15.Bean2 bean2 = new Test15.Bean2();
        bean2.j++;
        //Initialization 3
        Bean bean = new Bean();
        Bean.Bean3 bean3 = bean.new Bean3();
        bean3.k++;
    }
    class Bean1 {
        public int i = 0;
    }
    static class Bean2 {
        public int j = 0;
    }
}
class Bean {
    class Bean3 {
        public int k = 0;
    }

}

The creation of static internal class objects is generally external class. Internal class class name = new external class. Internal class ();
The creation of an internal class object of a member is generally an external class. Internal class class name = external class object name. new internal class ();

Topics: Java Back-end Programmer