loading Order of Java classes
The initialization rules for classes in Java are: Initialize the static member variable and the static block, initialize the non-static member variable and the non-static block, and finally initialize the constructor.
Example 1:
PackageDemo/** * This class mainly introduces the loading order of Java Classes * * / Public class testorder { Public Static intK =0; Public StaticTestorder T1 =NewTestorder ("T1"); Public StaticTestorder t2 =NewTestorder ("T2"); Public Static inti = print ("I"); Public Static intn = About;Private intA =0; Public intj = Print ("J"); {Print ("Building Blocks"); }Static{Print ("Static Block"); } Public Testorder(String str) {System.out.println (++k) +":"+ str +"i="+ i +"n="+ N); ++i; ++n; } Public Static int Print(String str) {System.out.println (++k) +":"+ str +"i="+ i +"n="+ N); ++n;return++i; } Public Static void Main(String args[]) {Testorder T =NewTestorder ("Init"); }}
The result is:
1:j i=0 n=0
2: Construction block I=1 n=1
3:t1 i=2 n=2
4:j i=3 n=3
5: Construction Block i=4 n=4
6:t2 i=5 n=5
7:i i=6 n=6
8: Static block i=7 n=99
9:j i=8 n=100
10: Construction Block i=9 n=101
11:init i=10 n=102
Loading order of Java classes