靜態變量初始化順序
1.簡單規則
首先先看一段最普遍的JAVA代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Test { public static Test1 t = new Test1(); public static int a = 0 ; public static int b; public static void main(String[] arg) { System.out.println(Test.a); System.out.println(Test.b); } } class Test1 { public Test1() { Test.a++; Test.b++; } } |
這里先猜下控制臺輸出結果是什么?
OK, 或許你已經猜到下面了結果了,那么你還是熟悉Java的。
如果你不明白是為什么會輸出上面的結果,那么我來告訴你。
Java靜態變量初始化遵循以下規則:
-
靜態變量會按照聲明的順序先依次聲明并設置為該類型的默認值,但不賦值為初始化的值。
-
聲明完畢后,再按聲明的順序依次設置為初始化的值,如果沒有初始化的值就跳過。
看了這個就會明白,原來Test.a的值變化了三次。
聲明時設置為0>>Test1::Test1里設置為1>>Test.a初始化為0
2.復雜規則
明白了這個,請再看下面的代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
public class A { public static int b = B.a; public static A plus = new A( "A" ); public static final int finalInt = ( int )(Math.random()* 100 ); public static B p = new B( "A" ); public static final String finalStr = "finalStr" ; public static final Integer finalInteger = new Integer( 10 ); public static int a = 1 ; public static B c = null ; public A(String from) { System.out.println( "----------- begin A::A ----------------" ); System.out.println( "A::A, from=" +from); System.out.println( "A::A, A.b=" +A.b); System.out.println( "A::A, A.finalInt=" +A.finalInt); System.out.println( "A::A, B.a=" +B.a); System.out.println( "A::A, B.plus=" +B.plus); System.out.println( "----------- end A::A ----------------" ); } public static void main(String[] arg) { System.out.println( "main, A.b=" +A.b); System.out.println( "main, B.t=" +B.t); System.out.println( "main, C.a=" +C.a); } } class B { public static int t = A.a; public static A plus = new A( "B" ); public static int a = 1 ; public B(String from) { System.out.println( "----------- begin B::B ----------------" ); System.out.println( "B::B, from=" +from); System.out.println( "B::B, B.a=" +B.a); System.out.println( "B::B, A.a=" +A.a); System.out.println( "B::B, A.p=" +A.p); System.out.println( "B::B, A.plus=" +A.plus); System.out.println( "B::B, A.finalInt=" +A.finalInt); System.out.println( "B::B, A.finalInteger=" +A.finalInteger); System.out.println( "B::B, A.finalStr=" +A.finalStr); System.out.println( "----------- end B::B ----------------" ); } } class C { public static final A a = new A( "C" ); } |
這個你還能猜到輸出結果嗎? 我是在一邊測試一邊寫的,所以我沒猜出來.哈哈
控制臺輸出結果為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
----------- begin A::A ---------------- A::A, from=B A::A, A.b=0 A::A, A.finalInt=0 A::A, B.a=0 A::A, B.plus=null ----------- end A::A ---------------- ----------- begin A::A ---------------- A::A, from=A A::A, A.b=1 A::A, A.finalInt=0 A::A, B.a=1 A::A, B.plus=A@a90653 ----------- end A::A ---------------- ----------- begin B::B ---------------- B::B, from=A B::B, B.a=1 B::B, A.a=0 B::B, A.p=null B::B, A.plus=A@1fb8ee3 B::B, A.finalInt=61 B::B, A.finalInteger=null B::B, A.finalStr=finalStr ----------- end B::B ---------------- main, A.b=1 main, B.t=0 ----------- begin A::A ---------------- A::A, from=C A::A, A.b=1 A::A, A.finalInt=61 A::A, B.a=1 A::A, B.plus=A@a90653 ----------- end A::A ---------------- main, C.a=A@61de33 |
這個結果你沒猜到吧,哈哈.
要一句一句的講解程序執行結果,還是要很到的篇幅的.這里就直接寫出Java靜態變量初始化遵循的規則了。
第一段的規則依然有效,只是不健全。
-
只有主動請求一個類,這個類才會初始化,僅包含靜態變量,函數,等靜態的東西.
-
繼承關系時,先初始化父類,后初始化子類.
-
靜態變量會按照聲明的順序先依次聲明并設置為該類型的默認值,但不賦值為初始化的值.
-
聲明完畢后,再按聲明的順序依次設置為初始化的值,如果沒有初始化的值就跳過.
-
當初始化A.b=B.a時,暫停初始化A.b,設置當前類為B,跳到步驟3,并執行.
-
當初始化B.plus = new A時,暫停初始化B.plus,實例化A并賦值給B.plus.
-
當A的構造函數里需要獲得B.a的值時,B.a還初始化并處于暫停初始化狀態,直接取B.a的當前值,不再等待B.a初始化.
-
final,靜態常量其實是遵循普通靜態變量的初始化的,但是在編譯時,編譯器會將不可變的常量值在使用的地方替換掉.可以用Java反編譯工具查看.
static數據的初始化
加上static限定的字段,是所謂的類字段,也就是說這個字段的擁有者不是對象而是類。無論創建多少對象,static數據都只有一份。
類內總是先初始化static字段,再初始化一般字段。接著初始化構造器。但是如果不創建這個類的對象,那這個對象是不會進行初始化的,并且只執行一次。
如下面的代碼,在StaticInitialization類中,先初始化static Table table = new Table();,然后才去初始化Table對象,不然是不會被初始化的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
class Bowl { Bowl( int marker) { print( "Bowl(" + marker + ")" ); } void f1( int marker) { print( "f1(" + marker + ")" ); } } class Table { static Bowl bowl1 = new Bowl( 1 ); Table() { print( "Table()" ); bowl2.f1( 1 ); } void f2( int marker) { print( "f2(" + marker + ")" ); } static Bowl bowl2 = new Bowl( 2 ); } class Cupboard { Bowl bowl3 = new Bowl( 3 ); static Bowl bowl4 = new Bowl( 4 ); Cupboard() { print( "Cupboard()" ); bowl4.f1( 2 ); } void f3( int marker) { print( "f3(" + marker + ")" ); } static Bowl bowl5 = new Bowl( 5 ); } public class StaticInitialization { public static void main(String[] args) { print( "Creating new Cupboard() in main" ); new Cupboard(); print( "Creating new Cupboard() in main" ); new Cupboard(); table.f2( 1 ); cupboard.f3( 1 ); } static Table table = new Table(); static Cupboard cupboard = new Cupboard(); } |
輸出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Bowl(1) Bowl(2) Table() f1(1) Bowl(4) Bowl(5) Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) f2(1) f3(1) |
顯示的靜態初始化(也就是靜態塊)
把多個初始化語句包在一個static花括號里,叫做靜態塊,其實就是把多個static合在一起寫了,本質是一樣的。只有首次創建對象或者首次訪問類的字段時才會執行,而且僅僅一次。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class Cup { Cup( int marker) { print( "Cup(" + marker + ")" ); } void f( int marker) { print( "f(" + marker + ")" ); } } class Cups { static Cup cup1; static Cup cup2; static { cup1 = new Cup( 1 ); cup2 = new Cup( 2 ); } Cups() { print( "Cups()" ); } } public class ExplicitStatic { public static void main(String[] args) { print( "Inside main()" ); Cups.cup1.f( 99 ); // (1) } // static Cups cups1 = new Cups(); // (2) // static Cups cups2 = new Cups(); // (2) } |
輸出:
1
2
3
4
|
Inside main() Cup(1) Cup(2) f(99) |
非靜態實例初始化
這個沒什么好講的,就是普通初始化,按順序執行,可以多次執行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class Mug { Mug( int marker) { print( "Mug(" + marker + ")" ); } void f( int marker) { print( "f(" + marker + ")" ); } } public class Mugs { Mug mug1; Mug mug2; { mug1 = new Mug( 1 ); mug2 = new Mug( 2 ); print( "mug1 & mug2 initialized" ); } Mugs() { print( "Mugs()" ); } Mugs( int i) { print( "Mugs(int)" ); } public static void main(String[] args) { print( "Inside main()" ); new Mugs(); print( "new Mugs() completed" ); new Mugs( 1 ); print( "new Mugs(1) completed" ); } } |
1
2
3
4
5
6
7
8
9
10
11
|
Inside main() Mug(1) Mug(2) mug1 & mug2 initialized Mugs() new Mugs() completed Mug(1) Mug(2) mug1 & mug2 initialized Mugs(int) new Mugs(1) completed |