Java internal class knowledge and Java class knowledge
Java internal class
I. Meaning
In Java programming language, a program is constructed by class. You can declare a class within a class. We call this class an internal class.
Ii. Functions
• Better encapsulation is implemented. We know that the access modifier of common classes (non-Internal classes) cannot be private or protected, but internal classes can. When we declare the internal class as private, only the external class can access the internal class, which hides the internal class.
• Internal classes can inherit (extends) or implement (implements) other classes or interfaces without being influenced by external classes.
• The internal class can directly access the fields and methods of the external class. Even if it is modified using private, the external class cannot directly access members of the internal class.
Iii. Principles
An internal class is a compilation concept. After compilation, two independent class files are generated, as shown below:
public class Outer{ private String outerName = "outer"; class Inner{ private String innerName = "inner"; } }
Compiled files include:
After compilation, Outer. Inner is renamed as Outer $ Inner, and the period (.) is replaced with the dollar sign ($ ).
Iv. Classification
Java internal classes can be divided into member Internal classes, local internal classes, anonymous internal classes, and static internal classes.
1) member internal class
The internal class of a member can be regarded as a member of an external class. static members cannot be declared in the internal class of the member, but the static final field is an exception. We know that when a class is loaded, static members will be initialized first. If the Member's internal class has static members, the internal class will be generated before the external class, and the internal class serves the external class, internal classes may be generated out of control before they are created. When instantiating a member's internal class, the member's internal class will hold a reference to the current object of the external class, so that the member's internal class can directly access the members of the External class, even the private modification.
Import static java. lang. system. out; public class Outer {private String outerName = "outer"; // The external class cannot directly access members of the internal class. You need to instantiate the internal class Object private Inner inner = new Inner (); public class Inner {private String innerName = "inner"; public void show () {out. println (outerName); // members of the external class can be directly accessed} public void show () {out. println (inner. innerName); inner. show ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (); // instantiate the internal class Outer. inner inner = outer. new Inner (); inner. show ();}}
Running result:
Inner
Outer
Outer
The internal class of a Member refers to the reference of the external class object, which is composed of the external class name before this. this form is called
Limitation-this, out. println (outerName) is equivalent to out. println (Outer. this. outerName.
2) Local internal class
The use of a local internal class is basically the same as that of a Member internal class, except that a local internal class is defined as a method of an external class, just like a local variable, and is not a member of an external class. Some internal classes cannot be accessed outside the method, but its instances can be returned from the method, and the instance will exist until it is no longer referenced. Local internal classes can also access local variables and method parameters of the method. The restriction is that local variables or method parameters can be accessed only when they are declared as final.
Import static java. lang. system. out; public class Outer {private String outerName = "outer"; public void show (final String str) {// The method parameter is final class Inner {public void print () {out. println (outerName + str) ;}} Inner inner = new Inner (); inner. print ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (": lalala ");}}
Running result:
Outer: lalala
3) Anonymous internal class
You can think of an anonymous internal class as a local internal class without a class name. An anonymous internal class has the following features:
1. No constructor is available for anonymous internal classes, and no class name is available for anonymous internal classes. Therefore, the constructor cannot be declared.
2. The anonymous internal class must inherit or implement an interface, and the new type must be the super type of the anonymous class. The Anonymous class cannot have the displayed extends or implements clause, there cannot be any modifier.
3. Anonymous internal classes are the same as member Internal classes and partial internal classes, and static members cannot be declared.
Import static java. lang. system. out; public class Outer {private String outerName = "outer"; public void show (final String str) {new Inner () {// implements the Inner interface public void print () {out. println (outerName + str );}}. print ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (": lalala") ;}} interface Inner {void print ();}
Running result:
Outer: lalala
4) static internal class
Static internal classes, also known as Nested classes in some books, must be declared with static modifiers. static internal classes are different from the first three internal classes, the static internal class does not hold the reference of the current object of the external class. Therefore, the static internal class cannot access non-static members of the external class. In this case, the static internal class does not depend on the external class.
Import static java. lang. system. out; public class Outer {private String outerName = "outer"; private static int id = 123; private Inner inner = new Inner (); public static class Inner {public void print1 () {// out. println (outerName); non-static member out of the external class cannot be accessed. println (id);} public static void print2 () {out. println (id) ;}} public void show () {inner. print1 ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (); Outer. inner. print2 (); // access the static internal class directly through the class name }}