Internal classes include member Internal classes, static nesting classes, method internal classes, and anonymous internal classes.
Common Characteristics of several internal classes:
A. The internal class is still an independent class. After compilation, the internal class will be compiled into an independent. Class file, but it is preceded by the class life and $ symbol of the external class.
B. Internal classes cannot be accessed in normal mode. An internal class is a member of an external class, so the internal class can freely access the member variables of the external class, whether it is private or not.
View code
1 /**
2 * Four different types of internal classes
3 * @ Author Administrator
4 *
5 */
6 Public Class Outerofinnerclass {
7 Int Out_x = 0;
8
9 Public Void Method (){
10 Inner1 inner1 = New Inner1 ();
11 Final Int A =-1;
12 // Internal classes defined within the method body can only use abstract or final state
13 /**
14 * Local internal class
15 *
16 */
17 Class Inner2 {
18 Public Void Method (){
19 Out_x = 3;
20 System. Out. println ();
21 }
22 }
23 Inner2 inner2 = New Inner2 ();
24 }
25 /**
26 * Member internal class inner class
27 *
28 */
29 Public Class Inner1 {
30
31 }
32 /**
33 * Static internal class static nested class
34 *
35 */
36 Public Static Class Inner3 {
37 Int A = New Outerofinnerclass (). out_x;
38 Public Void AA (){
39
40 }
41 }
42
43 Public Parentabstract RR (){
44 /**
45 * Anonymous internal class
46 */
47 Return New Parentabstract (){
48 Void Save (){
49
50 }
51
52 };
53 }
54
55 Public Void Testinner1 (){
56 Int B = 1;
57 Outerofinnerclass oo = New Outerofinnerclass ();
58
59 Outerofinnerclass. inner1 inner1 = oo. New Inner1 ();
60 }
61
62 }
63
64 Abstract Class Parentabstract {
65 Abstract Void Save ();
66 }
1. Member internal class: the form is as follows
Class outer {
Class inner {}
}
Compile the above Code Two files are generated: outer. Class and outer $ inner. Class.
No static declaration is allowed in the member's internal class! The following code cannot be compiled.
Class inner {
Static int A = 10;
}
The only way to access member Internal classes is through external class objects!
A. instantiate an internal class object from a non-static method of an external class.
Class Outer {
Private Int I = 10 ;
Public Void Makeinner (){
Inner in = New Inner ();
In. seeouter ();
}
Class Inner {
Public Void Seeouter (){
System. Out. Print (I );
}
}
}
On the surface, an internal class object is instantiated without creating an external class object, which is in conflict with the above. In fact, if you do not create an external class object, you cannot call the makeinner () method. Therefore, you must create an external class object in the end.
You may try to modify the makeinner () method to a static method, that is, static public void makeinner (). In this way, you can instantiate the external class without creating an external class! But can I access non-static members and methods in a static method? Obviously not. It does not reference this. Unable to jump out of that rule! But what if I instantiate an external class object in this static method and use this object to instantiate the external class? Yes! That is, the next content.
B. instantiate an internal class object from a static method of an external class.
Class outer {
Private int I = 10;
Class inner {
Public void seeouter (){
System. Out. Print (I );
}
}
Public static void main (string [] ARGs ){
Outer out = new outer ();
Outer. Inner in = out. New inner ();
// Outer. Inner in = new outer (). New inner ();
In. seeouter ();
}
}
The line commented out is the merge form of the above two rows, a concise statement.
Compare: In the non-static method of the external class, instantiating the internal class object is a common new method: inner in = new inner ();
To instantiate an internal class object in a static method of an external class, you must first create an external Class Object: outer. Inner in = new outer (). New inner ();
C. This reference of the internal class.
A common class can use this to reference the current object. The same applies to internal classes. But what if the internal class wants to reference the current object of the external class? In the form of "external Class Name". This;, the outer. This is shown in the following example.
Class outer {
Class inner {
Public void seeouter (){
System. Out. println (this );
System. Out. println (outer. This );
}
}
}
D. modifier of the member's internal class.
For common classes, available modifiers include final, abstract, strictfp, public, and default package access.
However, the internal class of a member is more like a member variable and method.
Available modifiers include final, abstract, public, private, protected, strictfp, and static.
Once the internal class is modified with static, it becomes a static internal class.
2. Internal class of the method.
Put the class in the method as the name suggests.
Class outer {
Public void dosomething (){
Class inner {
Public void seeouter (){
}
}
}
}
A. The internal class of a method can only be instantiated within the method that defines the internal class. It cannot be instantiated outside of this method.
B. The class object inside the method cannot use non-final local variables of the method of the internal class.
Because the local variable of the method is located on the stack, it only exists in the life cycle of the method. When a method ends, its stack structure is deleted, and local variables become history. However, after the method ends, the internal class object created in the method may still exist in the heap! For example, if a reference to it is passed to some other code and stored in a member variable. Because the survival of local variables cannot be guaranteed as long as that of class objects in the method, internal class objects cannot use them.
The following is a complete example:
Class outer {
Public void dosomething (){
Final int A = 10;
Class inner {
Public void seeouter (){
System. Out. println ();
}
}
Inner in = new inner ();
In. seeouter ();
}
Public static void main (string [] ARGs ){
Outer out = new outer ();
Out. dosomething ();
}
}
C. modifier of the internal class of the method.
Unlike the internal class of a member, the internal class of a method is more like a local variable.
Only final and abstract classes can be used to modify methods.
D. Internal class of the method in the static method.
The static method does not reference this. Therefore, the internal class in the static method suffers the same treatment, that is, it can only access the static members of the external class.
3. Anonymous internal class.
As the name suggests, there is no name for the internal class. On the surface, they seem to have names, but they are not actually their names.
A. inherited anonymous internal class.
Class car {
Public void drive (){
System. Out. println ("driving a car! ");
}
}
Class test {
Public static void main (string [] ARGs ){
Car car = new car (){
Public void drive (){
System. Out. println ("driving another car! ");
}
};
Car. Drive ();
}
}
The result is output: driving another car! The car reference variable is not a reference car object, but an object of the car anonymous subclass.
The key to building an anonymous internal class is to override one or more methods of the parent class. It is emphasized that the method of rewriting the parent class, rather than creating a new method. Because it is impossible to call methods that the parent class does not have by referencing the parent class! Creating a new method is unnecessary. In short, refer to polymorphism.
B. Interface-Based Anonymous internal class.
Interface vehicle {
Public void drive ();
}
Class test {
Public static void main (string [] ARGs ){
Vehicle v = new vehicle (){
Public void drive (){
System. Out. println ("driving a car! ");
}
};
V. Drive ();
}
}
The code above is strange, as if it is instantiating an interface. This is not the case. An interface-Based Anonymous internal class implements an interface-Based Anonymous class. Only one interface can be implemented.
C. Parameter-Based Anonymous internal class.
Class bar {
Void dostuff (FOO f ){}
}
Interface Foo {
Void Foo ();
}
Class test {
Static void go (){
Bar B = new bar ();
B. dostuff (New Foo (){
Public void Foo (){
System. Out. println ("foofy ");
}
});
}
}
4. Static nested class.
technically, static Nested classes do not belong to internal classes. Because the internal class shares a special relationship with the external class, it is more specifically the sharing relationship with the instance. Static Nested classes do not have the above relationship. It is only located inside another class, so it is also called a top-level nested class.
static means that the internal class can be accessed when there is no external class object, just like other static members. Static Nested classes cannot access members and methods of external classes.
class Outer {
static class inner {}< BR >}< br> class test {
Public static void main (string [] ARGs) {
outer. inner n = new outer. inner ();
}< BR >}