JavaSE -- parsing of anonymous objects, inner classes, static inner classes, local classes, and anonymous inner classes

As we all know, the class name of the external top-level class must be the same as the class file name, and only public and default can be used. The internal class refers to defining another class inside the external class. The class name does not need to be the same as the file name. Internal classes can be static, public, default, protected and private.

Anonymous object

Under normal circumstances, an object has heap memory space and stack memory space pointing to heap memory. If an object value has heap memory space but no stack memory pointing, such an object is called an anonymous object
**Benefits: * * it's convenient to use a new name
**Note: * * in this way, there is only heap memory space and no stack memory space

public class Test {
    private String name;
    public String getName(){
        return name;
    }

    public static void main(String[] args) {
        System.out.println(new Test2().getName());
    }
}

Internal class:

Concept:
The official name of the inner class is nested classes. Nested classes include static nested classes and inner classes. Internal classes are divided into member internal classes, local classes and anonymous classes.

Internal class is a concept of compilation. Once compiled successfully, it will become two completely different classes, namely outer Class and outer $inner Class. Therefore, the member variable / method name of the inner class can be the same as that of the outer class.

What is the role of inner classes?
1. Internal classes can be well hidden
General non internal classes are not allowed to have private and protected permissions, but internal classes can
2. The inner class has access rights to all elements of the outer class (private modifier can also access)
3. Multiple inheritance can be realized (multiple internal classes inherit multiple other classes respectively, so that the external class can obtain the properties of multiple other classes at the same time)
4. You can avoid modifying the interface and calling two methods with the same name in the same class. (external class inheritance, allowing internal classes to implement interfaces)

Static inner class

The shape is as follows:

public class OuterClass {
    private String name;

    static class StaticInerCls{
        private String name;
    }
}

The static inner class is no different from the outer class except that there are more access modifiers than the outer class, but the code organizes the static inner class in the outer class.
Create static inner class: class Form of iner

OuterClass.StaticInerCls staticInerCls = new OuterClass.StaticInerCls();  

Non static inner class

The non static inner class can access all members of the outer class, including private members. Although the external class cannot directly access the members of the internal class, it can access the private members of the internal class through the instance of the internal class.
Member inner class:
The shape is as follows:

public class OuterCls {
    private String name;
    public String getName(){
        return name;
    }

    class InerCls{
        private String name;

        public String getName(){
            return name;
        }
    }
}

Member inner classes can directly use all members and methods of outer classes, even if they are private decorated. The external class needs to access all member variables and methods of the internal class, and the internal class needs to be obtained through the object of the internal class. (who calls it his own son?) it should be noted that the internal class of a member cannot contain static variables and methods, because the internal class of a member needs to create an external class before it can create its own.
Create an internal class object method to object Form of new iner:

OuterCls outerCls = new OuterCls();
OuterCls.InerCls inerCls = outerCls.new InerCls();  

Member inner classes cannot have members decorated with static, but constants are allowed to be defined.

public class OuterClass {
    private String name;

    static class StaticInerCls{
        private String name;
    }

    class InerCls{
        private String name;
        private static int id;    //Not allowed, an error will be reported
        private static final int TYPE = 0;   //allow
    }
}

Local inner class

The inner class is defined in the method body and can only be used within the scope of the method or condition. It cannot be referenced after exiting the write scope.

public class Test {
    private String name;
    public String getName(){
        return name;
    }

    public void getInerCls(String name){
        class InerCls{
            private String name;

            public String getName(){
                return name;
            }
        }
        InerCls inerCls = new InerCls();
        inerCls.getName();
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.getName());//null
    }
}

As a special form of non static inner class, all restrictions of non static inner class also hold for local classes. The local class can access not only all members of the external class, but also the local variables of the method body, but it must be the local variables modified by final.

Why do local classes access local variables, and variables must be added with final?
Scenario:

    public static void main(String[] args){
        Outer out = new Outer();
        Object obj = out.method();
    }

    Object method(){
        int localVariable = 0;
        class Inner{
            void println(){
                System.out.println("localVariable " + localVariable);
            }
        }
        Object in = new Inner();
        return in;
    }
}

The localVariable here will turn red, indicating that the localVariable needs to be modified with final.
Resolution: This is a scope problem. After the method is executed, the local variable value becomes invalid, and the in object generated by new Inner() still has a reference to obj, so the object accesses a non-existent variable, which is not allowed. iner still exists. When the local variable is called externally and subsequently, the local variable may have been invalidated. But why can final be added to ensure access? Here, Java adopts the method of copy local variable. The variable defined as final will be copied and stored in the local internal class. Later, the object will be continuously maintained in the life cycle, so it can be accessed continuously.

Note: final can modify classes, methods and attributes. Modifier class, which cannot be inherited; Modifier method, which cannot be overridden by subclasses; Modifies a variable whose value cannot be modified.

Anonymous Inner Class

In order to avoid naming inner classes, or to use them only once, you can choose to use anonymous inner classes.

    public void countDown(){
        new Thread(){
            @Override
            public void run() {
            }
        }.start();
    }

Keywords: JavaSE

Added by DChiuch on Fri, 31 Dec 2021 19:27:46 +0200