Java inner class

First, the relationship between the inner class and the outer class

1. The inner class is defined in the outer class and is a relatively independent entity. Compiling the outer class file will generate two .class files, in which the .class file name of the inner class is the outer class name $inner class name .class.

2. The inner class can access all methods and properties of the outer class, even if it is private, because the inner class holds a reference to an outer class. The outer class cannot directly access the properties and methods of the inner class. It must be accessed through the object reference of the inner class. If it is a static inner class, the outer class can refer to the static variable through the class name of the inner class.

3. If you want to declare a static variable in an inner class, the inner class must be declared as a static class.

 

Second, the creation of non-static inner class objects

1. To create an inner class object outside the outer class, you need to create the outer class object first, and then call the inner class constructor through the outer class object to instantiate it. Objects of the outer class can only call the constructor of the inner class, and non-constructor methods and elements must be referenced by the inner class.

2. If you create an inner class object inside an outer class, you can directly call the constructor of the inner class to create it. Constructors can be called by external classes, other methods and properties must be called by internal class references or class names.

 

Third, the classification of inner classes

1. Member inner class: an ordinary class (non-static) defined in an outer class, a member of the outer class, and can access any variables and methods of the outer class.

2. Local inner class: Defined in the local scope of the outer class, such as defined in a method, only allows access to the inner class within the method, not outside the method. Local inner classes are generally used to aid in problem solving and it is not desirable to create a public class.

3. Anonymous inner class: An anonymous inner class is a local inner class without a class name, so it has no constructor, must be defined in the method, cannot be modified with access modifiers, cannot have static members, and must implement an abstract class or interface.

4. Static inner class: A non-static inner class will have a reference to the outer class after compilation, so it can call all attribute methods of the outer class. At the same time, non-static inner classes cannot have static variables. A static inner class does not have a reference to an outer class. A static inner class can only call the static members of the outer class and cannot access non-static members. The outer class can directly call the members of the static inner class.

 

Fourth, the creation of inner classes

1. Outside the external class: call new InnerClass() through the external class instance for instantiation

2. Inside the outer class: new InnerClass(), for static inner classes, there is no need to create them with the help of an instance of the outer class. For a non-static inner class, you need to create them with the help of an instance of the outer class.

class Test{
public static void getInnerClass1(){
new InnerClass1();//No need to create an instance of an external class
}
static class InnerClass1{
}
public static void getInnerClass2(){
new Test().new InnerClass2();//Required Create
}
class InnerClass2{
}
} with the help of an instance of the outer class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324629900&siteId=291194637