JVM - 类加载机制中的类初始化情况

回到顶部(go to top)

Main.java (总结在注释中)

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
57
58
59
60
public class Main {
 
     
    public static void main(String[] args) {
        /**
         * REASON:
         * 单纯的只初始化数组,不会触发相应的类实例化
         *
         * OUTPUT:
         * empty output
         */
        //SuperClazz[] sca = new SuperClazz[10];
 
 
        /**
         * REASON:
         * 对于“静态”(static)字段 -- 直接引用 / 静态字段引用静态字段
         *
         * 只有直接定义这个字段的类才会被初始化,因此通过其子类来引用父类中定义的静态字段,只会触发父类的初始化而不会触发子类的初始化。
         *
         * OUTPUT:
         * SuperClass init!
         * 123
         */
        //System.out.println(SubClazz.static_value);
        //System.out.println(SubClazz.static_value_ref_static);
 
 
        /**
         * REASON:
         * 对于“静态常量” (static final)字段 -- 直接引用 / 静态常量字段引用静态常量字段
         *
         * SuperClazz的“静态常量” (static final)字段 static_final_HELLOWORLD,其实在编译阶段通过“常量传播优化”,已经将SuperClazz的此常量值“hello world”存储到了SubClazz类的常量池中,
         * 以后SubClazz对常量HELLOWORLD的引用实际都被转化为SubClazz类对自身常量池的引用了。
         * 也就是说,实际上SubClazz的Class文件之中并没有SuperClazz类的符号引用入口,这两个类在编译成Class之后就不存在任何联系了.
         *
         * OUTPUT:
         * hello,world
         */
        //System.out.println(SubClazz.static_final_value);
        //System.out.println(SubClazz.static_final_ref_static_final);
 
 
        /**
         * REASON:
         * 对于混个状态下 -- 无论是“静态字段”引用“静态常量字段”, 还是“静态常量字段”引用“静态字段”
         * 都需要初始化SuperClazz,不能使用“常量传播优化”。
         * SubClazz的Class文件之中依然保存了SuperClazz类的符号引用入口。
         *
         * OUTPUT:
         * SuperClass init!
         * XXXXXXX(the real data)
         */
        //System.out.println(SubClazz.static_ref_static_final);
        //System.out.println(SubClazz.static_final_ref_static);
         
 
    }
 
}

  

  

 

回到顶部(go to top)

SuperClazz.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class SuperClazz {
     
    static {
        System.out.println("SuperClass init!");
    }
     
    public static int static_value = 123;
    public static final String static_final_value = "hello,world";
 
    public static int static_value_ref_static = static_value;
    public static final String static_final_ref_static_final = static_final_value;
 
    //-------------------
 
    public static String static_value2 = "static_value_STRING";
    public static final int static_final_value2 = 456;
 
    public static int static_ref_static_final = static_final_value2;
    public static final String static_final_ref_static = static_value2;
     
 
}

  

  

回到顶部(go to top)

SubClazz.java

1
2
3
4
5
6
7
public class SubClazz extends SuperClazz {
     
    static {
        System.out.println("Subclass init!");
    }
 
}

  

  

posted on   frank_cui  阅读(226)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示