Today read the Java Programming Idea (4th Edition Chinese version), just review the class of the initialization of the member of this knowledge point. In order to deepen the impression and give other people a little reference, examples in the reference book and modified to get the following code:
File Name: Helloworld.java
Package Name: Main
packagemain;classBowl {Bowl (intMarker) {System.out.println ("Bowl (" +marker+ ")"); } voidF1 (intMarker) {System.out.println ("F1 (" +marker+ ")"); }}classtable{StaticBowl BOWL1 =NewBowl (1); publicTable () {System.out.println ("Table ()"); BOWL2.F1 (1); } voidF2 (intMarker) {System.out.println ("f2 (" +marker+ ")"); } StaticBowl BOWL2 =NewBowl (2); }classCupboard{{System.out.println ("cupboard Class common code block execution"); } Bowl bowl3=NewBowl (3);//Note that this is not a static member StaticBowl Bowl4 =NewBowl (4); Static{System.out.println ("cupboard class static code block execution"); } publicCupboard () {//TODO auto-generated Constructor stubSystem.out.println ("cupboard ()"); BOWL4.F1 (2); } voidF3 (intMarker) {System.out.println ("F3 (" +marker+ ")"); } StaticBowl BOWL5 =NewBowl (5);} public classHelloWorld { public Static voidmain (String Args[]) {System.out.println ("Creating New Cupboard () in main"); NewCupboard (); TABLE.F2 (1); CUPBOARD.F3 (1); } StaticTable Table =NewTable (); StaticCupboard cupboard =NewCupboard ();}
Let's analyze the operation of the Program.
1. First the Java virtual machines want to execute the main method, must first load the class HelloWorld
2. found that the class HelloWorld contains static data (only static members), start initializing static members
First of all
"1" static Table table = new Table (); So the table class needs to be Loaded. found that the class contains static members and performs initialization operations according to the defined order of precedence
"2" static Bowl bowl1 = new Bowl (1); Output: Bowl (1)
"3" static Bowl bowl2 = new Bowl (2); Output: Bowl (2)
"4" constructor Table (), output: table () and F1 (1)
Next
"1" static cupboard cupboard = new cupboard (), So the cupboard class needs to be Loaded. The class is found to contain static data, including static members and static blocks of code, to perform initialization operations based on the order in which they are defined
"2" static Bowl bowl4 = new Bowl (4); output: Bowl (4)
"3"
static{
SYSTEM.OUT.PRINTLN ("cupboard class static code block execution");
}
Output: Cupboard class static code block executes
"4" static Bowl bowl5 = new Bowl (5); output: Bowl (5)
After static member initialization, the object is generated through NEW. When a class is found to have member definitions assigned and ordinary code blocks, the initialization operation is performed according to the order in which they are defined
"1"
{
System.out.println ("cupboard Class code block execution");
Output: Cupboard Generic code block execution
"2" Bowl bowl3 = new Bowl (3); output: Bowl (3)
"3" constructor cupboard (), output: cupboard () and F1 (2)
3. Enter the Main method
"1" System.out.println ("Creating New cupboard () in main"); Output: Creating New Cupboard () in main
"2" New Cupboard (); found that the cupboard class contains member definitions when assigned and ordinary code blocks, based on the sequence of the two definitions to perform initialization operations, Output: cupboard Class code block execution and Bowl (3)
"3" constructor Cupboard (), output: cupboard () and F1 (2)
"4" Table.f2 (1); Output: F2 (1)
"5" cupboard.f3 (1); Output: f3 (1)
In summary, the output of the program Is:
Bowl (1)
Bowl (2)
Table ()
F1 (1)
Bowl (4)
Cupboard class static code block execution
Bowl (5)
Cupboard Generic code block execution
Bowl (3)
Cupboard ()
F1 (2)
Creating new Cupboard () in main
Cupboard Generic code block execution
Bowl (3)
Cupboard ()
F1 (2)
F2 (1)
F3 (1)
The initialization of Java class data is summarized as Follows:
1. Static members and static code blocks are initialized in the order in which they are defined.
One thing to note here is that static code execution assumes that the Java virtual machines first load the class. The class is loaded the first time an object of a class is generated or when a static data member belonging to that class is first Accessed. In addition, static code blocks are initialized only once , while static members can be initialized multiple times, such as assigning values to member definitions first, and then assigning a value to a static block of Code.
2. Normal member definitions are assigned and normal code blocks are initialized in the order in which they are defined.
3. finally, The constructor method is initialized
Static members of the Java class, static blocks of code, ordinary members, ordinary blocks of code, the order in which the construction method initializes execution