[Java | Basics] Inner Classes

1. What is an inner class?

The inner class is to define another class in a class, and the inner class is also the embodiment of encapsulation. It can be declared as public、protected、private 或默认访问控制符. Inner classes can access all member variables and methods of outer classes, including private members. Inner classes can be used to implement some special functions, such as implementing callback functions, event handling, and so on. Common inner classes include instance inner classes (member inner classes), static inner classes, local inner classes, anonymous inner classes, etc.

2. Instance inner class

An instance inner class is a class defined inside an instance of another class. It is different from static inner class which is a static class defined inside another class. The inner class of the instance can access the instance variables and methods of the outer class, and can also define its own member variables and methods.

class Outer {
    
    
   	// ...
    class Inner {
    
    
       // ...
    }
    // ...
}

The Inner class in the above is the instance inner class.

The instantiation method of the inner class of the instance is as follows:
When using the inner class of the instance, it is necessary to first create an instance of the outer class, and then access the inner class through the instance of the outer class.
insert image description here

The inner class of the instance cannot define static member variables. If it is defined, it must be modified by final: the
insert image description here
inner class of the instance cannot define static methods:
insert image description here
if the member variable names of the inner class of the instance and the outer class are the same, then the inner class of the strength will first access its own member variable
insert image description here
instance Features of inner classes:

  1. The inner class of the instance cannot define static member variables, if defined, it must be modified by final
  2. Instance inner classes cannot define static methods
  3. If the member variable names of the instance inner class and the outer class are the same, then the instance inner class will first access its own member variables

3. Static inner class

A static inner class in Java refers to another class defined inside a class, and the inner class is declared as static. The difference between a static inner class and an ordinary inner class is that a static inner class does not depend on an instance of an outer class and can be accessed directly through the outer class name .

A static inner class is defined like this:

class OuterClass {
    
    
    // 外部类的成员和方法

    static class StaticInnerClass {
    
    
        // 静态内部类的成员和方法
    }
}

A static inner class is instantiated as follows:

OuterClass.StaticInnerClass innerObj = new OuterClass.StaticInnerClass();

Here are some points to note about static inner classes:

A static inner class can access the static members and methods of the outer class, but cannot access the non-static members and methods of the outer class .
Example:
insert image description here
At the same time, a static inner class can also define static members and methods, which are similar to the static members and methods of an outer class and can be accessed directly through the class name.
insert image description here
The method is the same and will not be demonstrated one by one.

Usage scenarios of static inner classes include but are not limited to:

  1. Encapsulating a class in another class makes the code of the outer class more concise and clear.
  2. Need to use a class in a static method of an outer class, but don't want to declare the class as a static member of the outer class.
  3. Need to access a class in the same package, but don't want to declare the class as public.

4. Local inner class

A Java local inner class is an inner class defined inside a method or code block. Unlike member inner classes, local inner classes can only be used inside the method or code block in which they are defined and cannot be accessed outside.

The syntax of a local inner class is as follows:

class OuterClass {
    
    
    // 外部类代码

    void outerMethod() {
    
    
        // 外部类方法

        class LocalInnerClass {
    
    
            // 局部内部类代码
        }

        // 使用局部内部类
        LocalInnerClass localInner = new LocalInnerClass();
    }
}

Features of local inner classes:

  1. 只能在定义它的方法或代码块内部使用,无法在外部访问
  2. Member variables and methods of the outer class, including private members, can be accessed.
  3. Local inner classes can access local variables in methods, but these variables must be of final type.
  4. Local inner classes can implement interfaces or inherit abstract classes.
  5. Local inner classes cannot define static members or methods, nor can they contain static code blocks.

Local inner classes are rarely used, just understand them.

5. Anonymous inner class

A Java anonymous inner class is a special inner class that has no class name and is defined and implemented directly when an object is created. It is usually used to create class objects that only need to be used once, which can simplify the code and improve the readability and maintainability of the code.

The syntax of an anonymous inner class is as follows:

new 类名/接口() {
    
    
    // 匿名内部类的类体部分
};

Example:

interface myInterface {
    
    
    public void func();
}

public class Demo1 {
    
    
    public static void main(String[] args) {
    
    
         new myInterface(){
    
    
             @Override
             public void func() {
    
    
                 System.out.println("方法的重写");
             }
         }.func();

    }
}
// 输出:
// 方法的重载

Is it a bit confusing to see this? Don't panic, look at the picture below:
insert image description here
This code defines an interface myInterface, which has an abstract method func(). Implemented myInterfacethe interface and overridden func()the method. Finally, call the overridden method by creating an object of the anonymous inner classfunc()

The use of inner classes includes inheritance or implementation, method rewriting, and object creation. The whole is a subclass object of a class or an implementation class object of an interface.

Usage scenario: When the parameter of the method is an interface or a class, taking the interface as an example, you can pass the implementation class object of this interface. If the implementation class only needs to be used once, you can use the anonymous inner class to simplify the code.

6. Conclusion

An important feature of inner classes in Java is to focus on mastering instance inner classes, static inner classes and anonymous inner classes.

Thank you for watching! I hope this article can help you!
Column: "Java Learning Journey from Scratch" is constantly being updated, welcome to subscribe!
"Wish to encourage you and work together!"
insert image description here

Guess you like

Origin blog.csdn.net/m0_63463510/article/details/130775640