偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Final關(guān)鍵字對(duì)JVM類加載器的影響

開發(fā) 后端
當(dāng)一個(gè)類中有聲明為static final的變量,這樣的變量對(duì)類的加載器有一定的影響,首先看看下面的例子。

當(dāng)一個(gè)類中有聲明為static final的變量,這樣的變量對(duì)類的加載器有一定的影響,首先看看下面的例子。

  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,在編譯的時(shí)候就可以知道它的值,所以直接訪問a的值不會(huì)引起FinalTest類的初始化。作為表現(xiàn),也就是static靜態(tài)代碼快不會(huì)被加載。

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

在看一個(gè)例子

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

這個(gè)static final變量a因?yàn)閕在編譯的時(shí)候無法知道它的確切的值,所以只有等到運(yùn)行的時(shí)候才能知道,所以自己訪問FinalTest4.a會(huì)引起FinalTest4類的初始化。也就是static靜態(tài)代碼快的加載。

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

  1. FinalTest4 static block  
  2. 82 

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

  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,會(huì)先初始化Parent類,然后初始化Child類。

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

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

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

  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變量,則只會(huì)直接初始化parent類,不會(huì)初始化Child類

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

  1. Parent static block
  2. 3

原文鏈接:http://blog.csdn.net/a352193394/article/details/7342583

【編輯推薦】

  1. 探究Java初始化的過程
  2. Java集合框架的知識(shí)總結(jié)
  3. Java與F#的并行程序處理對(duì)比
  4. Java并發(fā)編程之同步互斥問題
  5. Java中String.format的用法
責(zé)任編輯:林師授 來源: a352193394的博客
相關(guān)推薦

2020-08-10 08:00:13

JavaFinal關(guān)鍵字

2010-03-08 08:39:54

類加載器newJava

2021-01-05 10:26:50

鴻蒙Javafinal

2024-01-15 10:41:31

C++關(guān)鍵字開發(fā)

2009-12-08 18:02:06

PHP final關(guān)鍵

2019-08-28 16:38:49

finalJava編程語(yǔ)言

2021-02-17 13:35:17

finalgetJava

2009-11-26 19:24:54

PHP類CMS

2021-01-07 11:10:47

關(guān)鍵字

2021-01-26 07:20:26

Final關(guān)鍵字類變量

2024-04-08 11:35:34

C++static關(guān)鍵字

2009-06-04 09:15:46

2022-02-17 08:31:38

C語(yǔ)言staic關(guān)鍵字

2021-02-01 13:10:07

Staticc語(yǔ)言UNIX系統(tǒng)

2022-05-06 08:32:40

Pythonwith代碼

2025-01-09 10:30:40

2023-11-28 21:50:39

finalstaticvolatile

2024-03-15 15:12:27

關(guān)鍵字底層代碼

2022-11-12 18:32:50

Golangomitemptyjson

2009-06-25 10:33:53

StaticJava類
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)