Java類的構造順序,Java類構造順序
1.無物件建構
public class Test { public static int k = 0; public static int n = 99; public static int i = print("i"); static { print("靜態塊"); } { print("構造塊"); } public int j = print("j"); public Test(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[] strings) { } }
在載入類的時候構造靜態變數和靜態代碼塊。
順序按照定義的順序。
結果為:
1:i i=0 n=99
2:靜態塊 i=1 n=100
2.main函數中構造對象
public class Test { public static int k = 0; public static int n = 99; public static int i = print("i"); static { print("靜態塊"); } { print("構造塊"); } public int j = print("j"); public Test(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[] strings) { Test t = new Test("init"); } }
前兩條同上。
在構造對象時,先構造對象裡的非static變數,再調用建構函式。
非static類的變數按定義的順序構造。
結果為:
1:i i=0 n=99
2:靜態塊 i=1 n=100
3:構造塊 i=2 n=101
4:j i=3 n=102
5:init i=4 n=103
3.類中含static對象
public class Test { public static int k = 0; public static int n = 99; public static Test t1 = new Test("t1"); public static int i = print("i"); static { print("靜態塊"); } { print("構造塊"); } public int j = print("j"); public static Test t2 = new Test("t2"); public Test(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[] strings) { Test t = new Test("init"); } }
static類型變數、對象、代碼塊按定義的順序構造。
構造static對象時候,因為是構造對象所以同2所述,先構造非static變數,在調用建構函式。
結果:
1:構造塊 i=0 n=99
2:j i=1 n=100
3:t1 i=2 n=101
4:i i=3 n=102
5:靜態塊 i=4 n=103
6:構造塊 i=5 n=104
7:j i=6 n=105
8:t2 i=7 n=106
9:構造塊 i=8 n=107
10:j i=9 n=108
11:init i=10 n=109
4.static變數初始化
public class Test { public static int k = 0; public static Test t1 = new Test("t1"); public static int i = print("i"); public static int n = 99; static { print("靜態塊"); } { print("構造塊"); } public int j = print("j"); public static Test t2 = new Test("t2"); public Test(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[] strings) { Test t = new Test("init"); } }
先預設初始化再進行賦值。
結果:
1:構造塊 i=0 n=0
2:j i=1 n=1
3:t1 i=2 n=2
4:i i=3 n=3
5:靜態塊 i=4 n=99
6:構造塊 i=5 n=100
7:j i=6 n=101
8:t2 i=7 n=102
9:構造塊 i=8 n=103
10:j i=9 n=104
11:init i=10 n=105
java構造方法順序問題
如果一個類A繼承另外一個類B。那麼,在執行類A的構造方法(構造方法裡面不是supper()語句開始)的時候,就會先執行spuer(),也就是說,Chile(String name){}的構造方法,在編譯的時候,它會變成public Child(String name) {
super();
System.out.print(3);
this.name = name;
father = new People(name + ":F"); }
同樣,後面的估計就不用解釋了吧。都是同一個道理。super()表示的是該類的父類的無參構造方法
Java中類的初始化順序是什?
執行順序應該是:首先是父類靜態塊 --> 然後是子類靜態塊 --> 父類自由塊 --> 父類建構函式塊 --> 子類自由塊 --> 子類建構函式塊