The inner class literally means that there is another class in a class, the equivalent of an airplane, an airplane with an engine, and an engine with its own properties and methods, so the engine needs to be encapsulated with a class, and the engine is an inner class, and the airplane is the outer class of the engine. But in practical applications the inner class is not so well understood, the creation of an inner class object, and the external reference to it, so make the following summary.
Example 1: The basic structure of an inner class
1 //External Class2 classOut {3 Private intAge = 12;4 5 //Inner class6 classIn {7 Public voidprint () {8 System.out.println (age);9 }Ten } One } A - Public classDemo { - Public Static voidMain (string[] args) { theOut.in in =NewOut ().NewIn (); - in.print (); - //or access by sowing - /* + out = new Out (); - out.in in = Out.new in (); + in.print (); A */ at } -}
Operation Result: A
From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?
Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes
As the heart can directly access the body's blood, rather than through the doctor to draw the blood.
After compiling the program, two. class files are generated, respectively Out.class and Out$in.class
Where $ represents the out.in in the above program.
Out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note
1. The first out is to indicate which outer class the inner class object needs to be generated
2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class
Example 2: Variable Access form in an inner class
1classOut {2Private intAge = 12; 3 4classIn {5Private intAge = 13; 6 Public voidprint () {7intAge = 14; 8 System.out.println ("local variable:" +Age ); 9 System.out.println ("Inner class variable:" + This. age);Ten System.out.println ("Outer class variable:" + out.) This. age);11 }12 }13 }14 15 Public classDemo {16 Public Static voidMain (string[] args) {Out.in in =NewOut ().NewIn ();18in.print ();19 }20}
Operation Result:
Local variables: 14
Internal class variable: 13
External class variables: 12
As you can see from Example 1, an inner class that has no member variables and local variables of the same name, the inner class accesses the member variables of the outer class directly without specifying Out.this. Property name
Otherwise, local variables in the inner class will overwrite the member variables of the outer class
The member variable that accesses the inner class itself is available with the this. property name, and accessing the member variables of the external class requires the use of Out.this. Property name
Example 3: Static inner class
1 classOut {2 Private Static intAge = 12;3 4 Static classIn {5 Public voidprint () {6 System.out.println (age);7 }8 }9 }Ten One Public classDemo { A Public Static voidMain (string[] args) { -Out.in in =Newout.in (); - in.print (); the } -}
Operation Result: A
As you can see, if you statically internal static, the inner class can only access static member variables of the outer class, with limitations
Second, because the inner class is statically, the out.in can be viewed as a whole, and can be directly new to the object of the inner class (access to static through the class name, it doesn't matter if the external class object is generated)
Example 4: Private Inner class
1 classOut {2 Private intAge = 12;3 4 Private classIn {5 Public voidprint () {6 System.out.println (age);7 }8 }9 Public voidOutprint () {Ten NewIn (). print (); One } A } - - Public classDemo { the Public Static voidMain (string[] args) { - //This method is not valid - /* - out.in in = new Out (). New in (); + in.print (); - */ +Out of out =Newout (); A out.outprint (); at } -}
Operation Result: A
If an inner class only wants to be manipulated by a method in an external class, you can use private to declare the inner class
In the above code, we have to create an in class object inside the out class, and we can no longer use out.in in = new Out (). New in () creates an object of the inner class
That is, the inner class at this point is controlled only by the outer class
As is, my heart can only be controlled by my body, others cannot access it directly
Example 5: Method Inner class
1 classOut {2 Private intAge = 12;3 4 Public voidPrint (Final intx) {5 classIn {6 Public voidInprint () {7 System.out.println (x);8 System.out.println (age);9 }Ten } One NewIn (). Inprint (); A } - } - the Public classDemo { - Public Static voidMain (string[] args) { -Out of out =Newout (); -Out. Print (3); + } -}
Operation Result:
3
12
In the above code, we move the inner class into the method of the outer class, and then regenerate it into an inner class object in the method of the outer class to invoke the inner class method
If we need to pass in a parameter to a method in the outer class at this point, then the method parameter of the outer classes must use the final definition
As for final, there's no special meaning here, it's just a representation.
Java Internal classes