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

          當(dāng)一個類中有聲明為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. }  

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

          運(yùn)行結(jié)果為

          在看一個例子

          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因?yàn)閕在編譯的時候無法知道它的確切的值,所以只有等到運(yùn)行的時候才能知道,所以自己訪問FinalTest4.a會引起FinalTest4類的初始化。也就是static靜態(tài)代碼快的加載。

          運(yùn)行結(jié)果為

          1. FinalTest4 static block  
          2. 82 

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

          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. }  

          因?yàn)橹苯釉L問Child,b,會先初始化Parent類,然后初始化Child類。

          運(yùn)行結(jié)果為

          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類

          運(yùn)行結(jié)果如下

          1. Parent static block
          2. 3

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 安阳县| 镇平县| 通河县| 永仁县| 清新县| 象山县| 固镇县| 拉孜县| 凤山市| 海门市| 鄂州市| 资源县| 巧家县| 肃北| 历史| 古浪县| 仁寿县| 绿春县| 巩义市| 绍兴市| 军事| 定安县| 福安市| 塘沽区| 上林县| 盘锦市| 西吉县| 沅江市| 元朗区| 登封市| 灵台县| 利辛县| 防城港市| 郯城县| 略阳县| 包头市| 平湖市| 景德镇市| 叙永县| 盐亭县| 洪江市|