Building Blocks : Code enclosed in curly braces in the class member variable area, with no modification compared to the method, no return, no parameters;
static blocks : Add static decorations before building blocks
Static code blocks : Static blocks + static variables
non-static code block : Normal class member variable + building block
Program execution Order: static code block, non-static code block, class construction method
1 Public classTest {2 3 Static { 4System.out.println ("Static Block");5 } 6 7 { 8System.out.println ("Building Block");9 }Ten One PublicTest (String str) { ASystem.out.println ("constructor" +str); - } - the Public Static voidMain (string[] strings) { -Test T1 =NewTest ("T1"); -Test t2 =NewTest ("T2"); - } +}
The result of the execution is:
Static block construction block Constructors T1 building block Constructors T2
That is, the static block is executed before the main function, and the program can execute without the main function. )
Then the object is instantiated in main, then the program jumps to the constructor line, but does not directly execute the contents of the constructor, but begins to execute the non-static code block, and then executes the constructor after executing this part;
Also, from the result you can see that the static code block executes only once.
There is a special case where a static object
New one's own object in a static variable
1 Public classTest {2 3 Public Static intK = 0; 4 5 Public StaticTest T1 =NewTest ("T1"); 6 7 Public Static inti = print ("I"); 8 Public Static intn = 99;9 Ten Public intY=0; One Static { ASystem.out.println ("Static Block"); - } - the Public intY1=0; - Public intY2=0; - Public intY3=0; - Public intY4=0; + Public intJ1 = Print ("J1"); - { +System.out.println ("Building Block"); A } at - - PublicTest (String str) { -System.out.println ((++k) + ":" + str + "i=" + i + "n=" +N); -++i; -++N; in } - to Public Static intprint (String str) { +System.out.println ((++k) + ":" + str + "i=" + i + "n=" +N); -++N; the return++i; * } $ Panax Notoginseng Public Static voidMain (string[] strings) { -Test T =NewTest ("Init"); the } + A}
Execution Result:
1:j1 i=0 n=0 construction block 2:t1 i=1 n=13:i i=2 n=2 static block 4:j1 i=3 n=99 construction block 5:init i=4 n=100
From the results we found that "static block" is actually not the first output, debug debugging after the discovery of the execution sequence is
1.public static int k = 0;
2.public static Test T1 = new Test ("T1");
3.public Test (String str)//constructor, but does not execute content inside
4.public int y=0;
5.public int y1=0;
...
6.public int j1 = print ("J1");
7.system.out.println ("Building Block");
8.public Test (String str) {...} Content inside the constructor
It is true that static blocks are executed first, but when static variables are encountered
The program jumps to the constructor, but does not execute the contents, and then starts executing non-static blocks of code (note the public int y=0; is also a part of a non-static code block)
Then execute the constructor
That's why it's going to happen.
Here is the order of execution, draw a redraw between
Here is Ali's one-to-face question
One more place than the above
Both the static block and the construction block are output, and a new T2 object is added after the building block
But it doesn't matter, with the above analysis you can still get the results.
1 Public classTest {2 3 Public Static intK = 0; 4 5 Public StaticTest T1 =NewTest ("T1"); 6 7 Public Static inti = print ("I"); 8 Public Static intn = 99;9 Ten Static { OnePrint ("Static block"); A } - - Public intJ1 = Print ("J1"); the { -Print ("Building Block"); - } - + Public intj = Print ("J"); - + Public StaticTest t2 =NewTest ("T2"); A at PublicTest (String str) { -System.out.println ((++k) + ":" + str + "i=" + i + "n=" +N); -++i; -++N; - } - in Public Static intprint (String str) { -System.out.println ((++k) + ":" + str + "i=" + i + "n=" +N); to++N; + return++i; - } the * Public Static voidMain (string[] strings) { $Test T =NewTest ("Init"); Panax Notoginseng } -}
The result is:
1:j1 i=0 n=02: construction block I=1 n=13:j i=2 n=24:t1 i=3 n=35:i i=4 n=46: static block i=5 n=997:j1 i=6 n=1008: construction block i=7 n=1019:j i=8 n=10210 : T2 i=9 n=10311:j1 i=10 n=10412: construction block i=11 n=10513:j i=12 n=10614:init i=13 n=107
The first line of input code is
public int j = Print ("J");
-->system.out.println ((++k) + ":" + str + "i=" + i + "n=" + N);
This outputs the value of I as 0, according to the above analysis, the initialization of the variable i is
public static int i = print ("i");
This line has not been executed yet, so why not error?
Java static block and construction block execution sequence analysis learning