I. The relationship between an inner class and an external class
1, the inner class is defined in the external class, is a relatively independent entity, the external class file is compiled, will produce two. class files, where the inner class of the. class file name is the external class name $ internal class name.
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 external class. External classes cannot directly access the properties and methods of an inner class, they must be accessed through an object reference to an inner class, and, in the case of a static inner class, the external class can refer to a static variable through the class name of the inner class.
3. Inner class If you want to declare a static variable, the inner class must be declared as a static class.
Ii. creation of non-static inner class objects
1. To create an inner class object outside the outside class, you need to first create an external type object and instantiate it by invoking the constructor of the inner class through the external class object. An external class object can only invoke the constructor of an inner class, and the non-constructed method and the element must be referenced by an inner class.
2. If you create an inner class object inside an external class, you can call the construction method of the inner class directly to create it. Construction methods can be called by external classes, and other methods and properties must be called through an intrinsic class reference or class name.
III. Classification of internal classes
1. member Inner class: A normal class (not static) defined in an external class that belongs to the members of an external class and can access any variables and methods of the outer class.
2. Local inner class: defined in the local scope of an external class, for example, defined in a method, allowing access to the inner class only in the method, not outside of the method. Local inner classes are typically used in scenarios that help solve problems without creating a common class.
3. Anonymous inner class: the anonymous inner class is a local inner class without a class name, so it has no constructor method, must be defined in the method, cannot be decorated with an access modifier, cannot have a static member, and must implement an abstract class or interface.
4. Static inner class: a non-static inner class will have a reference to an external class after compilation, so it can invoke all of the property methods of an external class, while a non-static inner class cannot have a static variable. Static inner classes do not have references to external classes, static inner classes can only invoke static members of external classes, cannot access non-static members, and external classes can invoke members of static inner classes directly
Iv. Creation of internal classes
1. Outside class: Instantiate by calling New Innerclass () from an instance of an external class
2, External class: New Innerclass (), for static inner class, do not need to use external class instance to create, for non-static inner class, need to use external class instance to create.
Class test{
public static void GetInnerClass1 () {
New InnerClass1 ();//Do not need to create with an external class instance
}
Static Class innerclass1{
}
public static void GetInnerClass2 () {
New Test (). New InnerClass2 ();//need to create with external class instance
}
Class innerclass2{
}
}
Java Internal classes