JAVA class loading sequence
JAVA class loading sequence
Class initialization rules in JAVA are as follows: Initialize static member variables and static blocks first, then initialize non-static member variables and non-static blocks, and finally initialize constructors.
Example 1:
Package demo;/*** this class mainly describes the loading sequence of java classes **/public class TestOrder {public static int k = 0; public static TestOrder t1 = new TestOrder ("t1"); public static TestOrder t2 = new TestOrder ("t2"); public static int I = print ("I "); public static int n = 99; private int a = 0; public int j = print ("j"); {print (" ");} 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 = new TestOrder ("init ");}}
The result is:
1: j I = 0 n = 0
2: Construct block I = 1 n = 1
3: t1 I = 2 n = 2
4: j I = 3 n = 3
5: Construct block I = 4 n = 4
6: t2 I = 5 n = 5
7: I = 6 n = 6
8: static block I = 7 n = 99
9: j I = 8 n = 100
10: Construct block I = 9 n = 101
11: init I = 10 n = 102