posts - 110, comments - 101, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          當一個類中有聲明為static final的變量,這樣的變量對類的加載器有一定的影響,首先看看下面的例子。

          1. package com.bird.classLoad;  
          2.  
          3. class FinalTest{  
          4.       
          5.     public static final int a = 6/3;  
          6.       
          7.     static{  
          8.         System.out.println("FinalTest static block");  
          9.     }  
          10. }  
          11.  
          12. public class Test3 {  
          13.     public static void main(String[] args) {  
          14.         System.out.println(FinalTest.a);  
          15.     }  
          16. }  

          因為a是static final變量,且它等于6/3,在編譯的時候就可以知道它的值,所以直接訪問a的值不會引起FinalTest類的初始化。作為表現,也就是static靜態代碼快不會被加載。

          運行結果為

          在看一個例子

          1. package com.bird.classLoad;  
          2.  
          3. import java.util.Random;  
          4.  
          5. class FinalTest4{  
          6.       
          7.     public static final int a = new Random().nextInt(100);  
          8.       
          9.     static{  
          10.         System.out.println("FinalTest4 static block");  
          11.     }  
          12. }  
          13.  
          14. public class Test4 {  
          15.  
          16.     public static void main(String[] args) {  
          17.         System.out.println(FinalTest4.a);  
          18.     }  
          19. }  

          這個static final變量a因為i在編譯的時候無法知道它的確切的值,所以只有等到運行的時候才能知道,所以自己訪問FinalTest4.a會引起FinalTest4類的初始化。也就是static靜態代碼快的加載。

          運行結果為

          1. FinalTest4 static block  
          2. 82 

          下面的例子是講,當子類被主動訪問的時候,會引起其直接父類的初始化

          1. package com.bird.classLoad;  
          2.  
          3. class Parent{  
          4.       
          5.     static int a = 3;  
          6.       
          7.     static{  
          8.         System.out.println("Parent static block");  
          9.     }  
          10. }  
          11.  
          12. class Child extends Parent{  
          13.       
          14.     static int b = 4;  
          15.     static{  
          16.         System.out.println("Chind static block");  
          17.     }  
          18. }  
          19.  
          20. public class Test5 {  
          21.       
          22.     public static void main(String[] args) {  
          23.         System.out.println(Child.b);  
          24.           
          25.     }  
          26. }  

          因為直接訪問Child,b,會先初始化Parent類,然后初始化Child類。

          運行結果為

          1. Parent static block  
          2. Chind static block  
          3. 4 

          如果通過子類直接訪問父類的變量,只會初始化父類而不會初始化子類

          1. package com.bird.classLoad;  
          2.  
          3. class Parent{  
          4.       
          5.     static int a = 3;  
          6.       
          7.     static{  
          8.         System.out.println("Parent static block");  
          9.     }  
          10. }  
          11.  
          12. class Child extends Parent{  
          13.       
          14.     static{  
          15.         System.out.println("Chind static block");  
          16.     }  
          17. }  
          18.  
          19. public class Test5 {  
          20.       
          21.     public static void main(String[] args) {  
          22.         System.out.println(Child.a);  
          23.           
          24.     }  
          25. }  

          直接訪問Parent類的a變量,則只會直接初始化parent類,不會初始化Child類

          運行結果如下

          1. Parent static block
          2. 3

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 东城区| 鹤岗市| 伽师县| 威信县| 伊春市| 高碑店市| 西吉县| 古浪县| 霍州市| 日照市| 盐源县| 韩城市| 镇雄县| 西畴县| 吴忠市| 大埔区| 灵寿县| 教育| 磴口县| 大渡口区| 吉首市| 婺源县| 宁河县| 南投市| 当雄县| 赣州市| 都兰县| 利川市| 泰宁县| 武山县| 和政县| 苏尼特左旗| 桦甸市| 大石桥市| 永宁县| 容城县| 吉水县| 桃江县| 志丹县| 永平县| 巴楚县|