In the "in-depth analysis of Java internals" You can learn something about the Java inner class, but there are some places in the inner class that deserve our careful study ...
Here are some of the things that I've summed up in the Java inner class that I've shared with you guys ....
One: Static inner classes can have static members, but not static inner classes cannot have static members
How do you understand this?
Look at the following code:
Copy Code code as follows:
/**
*
*/
Package com.b510.test;
public class Test {
private int number = 1;
Non-static inner classes can have non-static members
Private class Innertest {
Error non-static inner class cannot have static members
private static int innumber = 2;
private int innumber = 2;
Public Innertest () {
Setnumber (2);
Innumber = innumber + number;
System.out.println ("innertest---" + innumber);
}
}
Private method of Test
private void Setnumber (int number) {
This.number = number;
}
Constructors
Public Test () {
Innertest in = new Innertest ();
SYSTEM.OUT.PRINTLN ("test");
}
public static void Main (string[] args) {
Test test = new test ();
Innertest---4
Test
}
}
The first concept is not very well understood ....
Two: non-static members of static inner classes can access static variables of external classes, and non-static variables of external classes are inaccessible
This involves the relationship between the static internal class and the external class:
Copy Code code as follows:
/**
*
*/
Package com.b510.test;
public class Test {
private static int number = 1;
Private String name = "Test";
Static Inner class
private Static Class Innertest {
Static inner classes can have non-static members
private int innumber = 2;
Public Innertest () {
Static inner classes can access static members of external classes
Setnumber (2);
Innumber = innumber + number;
System.out.println ("innertest---" + innumber);
Error static inner class cannot access non-static members of external classes
SYSTEM.OUT.PRINTLN (name);
}
}
Static Private method of test
private static void Setnumber (int n) {
Number = n;
}
Constructors
Public Test () {
Innertest in = new Innertest ();
SYSTEM.OUT.PRINTLN ("test");
}
public static void Main (string[] args) {
Test test = new test ();
Innertest---4
Test
}
}
This is actually very good understanding, do not know you look code 15~23 have no understanding ....
Three: non-static members of non-static inner classes can access non-static variables of external classes
This is already mentioned in the first: 17 lines in one code
Copy Code code as follows:
1 Innumber = innumber + number;
Number is a non-static member of the outer class, and Innumber can access the number as a member of a non-static inner class
Is it well understood ....
To sum up: