[JAVA] java internal class analysis details, java class analysis details
Internal class:
A normal class is defined in. the outermost layer of a java text file, that is, defines a class. This class does not contain any element of the {} scope, but the internal class is a class defined inside a class.
Why does java provide such a class definition method? If you want to define a class as usual, either public class Cls or class Cls. If you want to use these classes, you can directly reference them, however, if you need to define a class for programming, you only want to use this class for a class. If you do not want this class to be used by others, you will use the internal class.
Java has four internal classes:
1. Non-static internal class
A) Member internal class member inner class
B) Local inner class
C) Anonymous internal class anonymous inner class
2. Static internal class static inner class (nested class)
Non-static internal class
A non-static internal class is a class without the static keyword.
Used when an internal class needs to interact with its external class
Member internal class:
The position defined by the internal class is in the member position of the class, that is, inside {} of the outermost layer of the class
The internal class of the member can ensure that all the members of the Internal class can access the external class including private, including static
Access outside external class: External class. Internal class
New outside external class: Use oneExisting internal Class ObjectNew p. new Inner ()
External class members accessing the internal class: External class. this. Member
Local internal class:
Class defined in a member method, similar to a local variable
It is used in the same way as a local variable and cannot be modified using public.
Can access the final variables defined elsewhere in the method. Non-final variables are not allowed.
Members who can access the class of the Method
It can only be used in the scope of this method, that is, external classes cannot be used, and external classes cannot be used.
Static internal class:
Static internal classes are nested classes.
The internal class can only access static members of the external class (this is different from the internal class of the member), because static is in the static storage area
Declare external class outside external class. Internal class
In the out-of-class new, you can directly new external class. Internal class, because there is no object.
Anonymous internal class:
Internal class without name
An object is generated during definition and used only once.
It is often used to input a parameter to a function.
Defines an anonymous internal class, which must be defined when a parent class or interface persists.
The definition format is new parent class or interface () {member };
The anonymous internal class is anonymous, so there is no constructor. To implement the constructor function, use the constructor code block {}
The anonymous internal class can only access final local variables, because it is actually a variant of the local internal class.
When an anonymous internal class is created, the constructor of the parent class is implicitly called. If the constructor of the parent class has parameters, you must input (x) in the brackets () without final.
Note:
The underline indicates that there are still doubts.
Java has four internal classes:
1. Non-static internal class
A) Member internal class member inner class
B) Local inner class
C) Anonymous internal class anonymous inner class
2. Static internal class static inner class (nested class)
Internal class:
An internal class is a class defined within a class.
When programming, You need to define a class, but only want to use a class.
Non-static internal class
A non-static internal class is a class without the static keyword.
Used when an internal class needs to interact with its external class
Member internal class:
The position defined by the internal class is in the member position of the class, that is, inside {} of the outermost layer of the class
The internal class of the member can ensure that all the members of the Internal class can access the external class including private, including static
Access outside external class: External class. Internal class
New outside external class: use an existing internal class object to new p. new Inner ()
External class members accessing the internal class: External class. this. Member
Local internal class:
Class defined in a member method, similar to a local variable
It is used in the same way as a local variable and cannot be modified using public.
Can access the final variables defined elsewhere in the method. Non-final variables are not allowed.
Members who can access the class of the Method
It can only be used in the scope of this method.