In the "in-depth analysis based on the internal functions of java classes", you can understand some of the internal classes of java, but there are some points in the internal classes that deserve our careful research...
The following are some of the java internal classes I have summarized and shared with everyone ....
One: static internal classes can have static members, but non-static internal classes cannot have static members.
How can we understand this?
Take a look at the following code:
Copy codeThe Code is as follows :/**
*
*/
Package com. b510.test;
Public class Test {
Private int number = 1;
// Non-static internal classes can have non-static members
Private class InnerTest {
// Error non-static internal 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 );
}
}
// Test private Method
Private void setNumber (int number ){
This. number = number;
}
// Constructor
Public Test (){
InnerTest in = new InnerTest ();
System. out. println ("test ");
}
Public static void main (String [] args ){
Test test = new Test ();
// InnerTest --- 4
// Test
}
}
Is the first concept well understood .....
Two: non-static members of the static internal class can access static variables of the external class, but cannot access non-static variables of the external class.
The relationship between static internal classes and external classes is involved here:Copy codeThe Code is as follows :/**
*
*/
Package com. b510.test;
Public class Test {
Private static int number = 1;
Private String name = "test ";
// Static internal class
Private static class InnerTest {
// The static internal class can have non-static members
Private int inNumber = 2;
Public InnerTest (){
// The static internal class can access the static members of the external class
SetNumber (2 );
InNumber = inNumber + number;
System. out. println ("innerTest ---" + inNumber );
// Error static internal class cannot access non-static members of the external class
// System. out. println (name );
}
}
// Test static private Method
Private static void setNumber (int n ){
Number = n;
}
// Constructor
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 understandable. I don't know what code you think is 15 ~ 23 does not understand ....
Three: non-static members of the non-static internal class can access non-static variables of the external class.
This is already mentioned in the first article: 17 lines of one codeCopy codeThe Code is as follows: 1 inNumber = inNumber + number;
Number is a non-static member of an external class. As a member of a non-static internal class, inNumber can access the number
Is it easy to understand ....
Summary: