Java inner class? That's enough

Posted by arag0rn on Sat, 05 Feb 2022 14:04:13 +0100

catalogue

1, Member inner class

concept

Generation of class objects within members

The internal syntax of an external class is the same as that of an ordinary class.

External class: the premise is that the internal class is visible to the external class.

Member internal class code example

2, Static inner class

concept

Generation of static internal class objects

Internal of external class: the usage is the same as that of ordinary class

External class: external class Internal class reference = new external class Internal class ();

Static internal class code example

3, Method inner class (local inner class) - analogy local variable

concept

Method internal class code example

4, Anonymous inner class (predecessor of lambda expression. Functional programming)

concept

Anonymous class internal code example

1, Member inner class

concept

An internal class member method or property defined directly in a class without static definition. The inner class of the member must depend on the existence of the outer class. The inner class of the member can be generated only after there is an outer class object.

Generation of class objects within members

The internal syntax of an external class is the same as that of an ordinary class.

Inner in = new Inner0;

External class: the premise is that the internal class is visible to the external class.

Outter.Inner in = new Outter().new Inner0;

New outer() generates an external class object, and then constructs an internal class object through this object = > when an internal class object is generated, the external class object constructing this internal class will be passed into the internal class by the compiler.

Internal and external classes can easily access each other's private domains.
The internal class accesses the private domain of the external class directly (an external class object is hidden in the internal class).

Member methods can access static fields and cannot define static properties.
Can you define a static variable for a class inside a member? A: No, the internal class of a member must depend on the external class. If the internal class of a member has static properties, it can be accessed without external class objects.

class Inner {
   private static int test = 10;//report errors
}

For external classes, can member inner classes be used in static methods of external classes? Answer: No, it is equivalent to calling member variables in static methods.
For example: create an internal class object in the main of the external class

public static void main(String[] args) {
    Inner inner = new Inner();//report errors
}

Member internal class code example

public class Outter {
    private String msg = "outter Class msg attribute";
    // The heart and engine belong to private internal classes, which are completely hidden from the outside and only used inside the class
    // ---------------------------
    class Inner {
        private int num = 10;
        private String msg = "Inner class msg attribute";
        public void test() {
            // Direct access to msg properties of external classes
            // The private member variable msg of the external class is directly accessed here, and the member variable must be accessed through the object
            System.out.println(Outter.this.msg);
        }
    }
    // ---------------------------
    public void fun() {
        // Access the private properties of the inner class through the inner class object
        Inner inner = new Inner();
        inner.test();
    }

    public static void main(String[] args) {

    }
}

2, Static inner class

concept

Defined in a class, an inner class decorated with static is a static inner class. Static internal classes do not need to rely on external class objects!

Generation of static internal class objects

Internal of external class: the usage is the same as that of ordinary class

//The member method creates the inner class object of the member
public void test(){
   Inner inner = new Inner();
}

//Static method to create member inner class object inner
public static void main(String[] args) {
   Inner inner = new Inner();
}

Why can both member methods and static methods create class objects inside members?
A: analogy with static variables in a class, objects without a class can be used, static methods of a class can be called, and so can member methods.

External class: external class Internal class reference = new external class Internal class ();

Outter1. Inner inner = new outter1. Inner() ; / / there is no need to generate external class objects at this time

Static inner class is an ordinary class, which is just set inside a class.
Can static inner classes have member variables? Can I access member variables of external classes?
A: can ordinary classes define their own member variables? Static inner classes can have member domains. No, the member variables of the external class can only be accessed by objects. At this time, the internal class has no external class objects, so it cannot be accessed directly.

Static internal class code example

//Demonstration of static inner classes
public class Outter1 {
    private static int test = 100;
    private String msg = "Member variables of external classes";
    //Static inner class is a common class, which is just set in outer1
     static class Inner{
        static int num = 10;
        int age = 3;
        public void fun(){
//            System.out.println(msg);// Program error
            System.out.println(new Outter1().msg);//You can access the member domain through the new external class object
            System.out.println(test);
        }
    }

    //The member method creates the inner class object of the member
    public void test(){
        Inner inner = new Inner();
    }

    //Static method to create member inner class object inner
    public static void main(String[] args) {
        Inner inner = new Inner();
        inner.fun();
    }
}

The inner class of a member can access the member domain and static domain of the outer class, but cannot own the static domain.
The static internal class can have a member domain, but it cannot directly access the member domain of the external class. You can access the static domain through new external class objects.

3, Method inner class (local inner class) - analogy local variable

concept

Classes directly defined inside the method are not allowed to use any access modifiers and are completely hidden from the outside (without this method, this class is gone).
Method inner classes cannot define static fields. In addition, the usage is basically the same as that of member inner classes.
If a method parameter is used in the internal class of a method, the parameter is an implicit final declaration (after JDK8 and before JDK8, if a method parameter is used in the internal class of a method, the parameter must use the final declaration)

Method internal class code example

//Method inner class example
public class Outter2 {
    private int age = 10;
    //Nesting an inner class inside a method
    public void fun(int num){
        //Method. No access modifiers private, public and static are allowed
        class Inner{
            public void test(){
                System.out.println(age);
                System.out.println(num);
//                //Errors are reported in the following two lines
//                num++;
//                System.out.println(num);// Local variables referenced from internal classes must be final or actual final variables
            }
        }
        //When Inner uses the variable num, Num becomes implicit final, and the value cannot be modified
//        num ++;
        System.out.println(num);

        //Create method internal class object
        Inner inner = new Inner();
        //Call the test method of the inner class of the method
        inner.test();
    }
    public static void main(String[] args) {
        Outter2 outter2 = new Outter2();
        outter2.fun(20);
    }
}

//results of enforcement
//20
//10
//20

4, Anonymous inner class (predecessor of lambda expression. Functional programming)

concept

Anonymous inner classes comply with all the requirements inside the method. By default, anonymous inner classes inherit a class (both ordinary classes and abstract classes, generally inherit abstract classes or implement interfaces) or implement an interface.

Anonymous inner class code example

//Anonymous Inner Class 
public class Outter3 {
    public static void fun(IMessage msg){
        msg.printMsg();
    }

    public static void main(String[] args) {
        fun(new IMessage() {
            //This is an anonymous inner class
            //It is equivalent to creating a class, implementing the Imessage interface, and creating the object of this class
            @Override
            public void printMsg() {
                System.out.println("Usage of anonymous inner classes");
            }
        });
//        IMessage msg = new IMessageImpl();
//        fun(msg);
    }
}
interface IMessage{
    //Define abstract methods
    void printMsg();
}
class IMessageImpl implements IMessage{
    @Override
    public void printMsg() {
        System.out.println("Common usage");
    }
}

End of this section^_^

Topics: Java Back-end