Final关键字对JVM类加载器的影响

开发 后端
当一个类中有声明为static final的变量,这样的变量对类的加载器有一定的影响,首先看看下面的例子。

当一个类中有声明为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

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

【编辑推荐】

  1. 探究Java初始化的过程
  2. Java集合框架的知识总结
  3. Java与F#的并行程序处理对比
  4. Java并发编程之同步互斥问题
  5. Java中String.format的用法
责任编辑:林师授 来源: a352193394的博客
相关推荐

2020-08-10 08:00:13

JavaFinal关键字

2010-03-08 08:39:54

类加载器newJava

2021-01-05 10:26:50

鸿蒙Javafinal

2024-01-15 10:41:31

C++关键字开发

2009-12-08 18:02:06

PHP final关键

2019-08-28 16:38:49

finalJava编程语言

2021-02-17 13:35:17

finalgetJava

2009-11-26 19:24:54

PHP类CMS

2021-01-26 07:20:26

Final关键字类变量

2021-01-07 11:10:47

关键字

2024-04-08 11:35:34

C++static关键字

2023-11-28 21:50:39

finalstaticvolatile

2009-06-04 09:15:46

2009-06-25 10:33:53

StaticJava类

2009-12-10 13:31:20

PHP self关键字

2022-05-06 08:32:40

Pythonwith代码

2021-02-01 13:10:07

Staticc语言UNIX系统

2022-02-17 08:31:38

C语言staic关键字

2024-03-15 15:12:27

关键字底层代码

2022-11-12 18:32:50

Golangomitemptyjson
点赞
收藏

51CTO技术栈公众号