java踩坑记-static final 顺序

本文揭示了一个关于单例模式中实例化顺序的问题,在特定条件下会导致NullPointerException。问题出现在一个类在自身构造过程中尝试访问另一个尚未完成初始化的单例对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个坑是关于单例模式 getInstance时候的static final顺序,而且问提十分隐蔽。在实际的项目,在QA环境下由于判断条件为false,无法发现此问题;而到生产上之后,判断条件为true,抛了空指针异常。

情况1:判断条件为false
 1 package com.paypal.dinyu;
 2 
 3 /**
 4  * Created by dinyu on 26/07/2017.
 5  */
 6 public class Bpp {
 7     private static final Bpp INSTANCE = new Bpp();
 8     private static final Cpp CPP = Cpp.getInstance();
 9 
10     private Bpp() {
11         init();
12     }
13 
14     public static Bpp getInstance() {
15         return INSTANCE;
16     }
17 
18     private void init() {
19         if (false) {
20             System.out.println("CPP.toString: " + CPP.toString());
21         }
22     }
23 
24     public static void main(String[] args) {
25         Bpp.getInstance();
26     }
27 }
28 
29 class Cpp {
30     private static final Cpp INSTANCE = new Cpp();
31 
32     private Cpp() {
33 
34     }
35 
36     public static Cpp getInstance() {
37         return INSTANCE;
38     }
39 }

Output:

Process finished with exit code 0

情况2:判断条件为true
 1 package com.paypal.dinyu;
 2 
 3 /**
 4  * Created by dinyu on 26/07/2017.
 5  */
 6 public class Bpp {
 7     private static final Bpp INSTANCE = new Bpp();
 8     private static final Cpp CPP = Cpp.getInstance();
 9 
10     private Bpp() {
11         init();
12     }
13 
14     public static Bpp getInstance() {
15         return INSTANCE;
16     }
17 
18     private void init() {
19         if (true) {
20             System.out.println("CPP.toString: " + CPP.toString());
21         }
22     }
23 
24     public static void main(String[] args) {
25         Bpp.getInstance();
26     }
27 }
28 
29 class Cpp {
30     private static final Cpp INSTANCE = new Cpp();
31 
32     private Cpp() {
33 
34     }
35 
36     public static Cpp getInstance() {
37         return INSTANCE;
38     }
39 }

Output:

Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.NullPointerException
at com.paypal.dinyu.Bpp.init(Bpp.java:20)
at com.paypal.dinyu.Bpp.<init>(Bpp.java:11)
at com.paypal.dinyu.Bpp.<clinit>(Bpp.java:7)
... 3 more

Process finished with exit code 1

出现以上情况的原因,是因为在Bpp.getInstace()时,在Bpp中new了自己,而在new的过程中又需要用到Cpp的Instance,但是Cpp的static块在Bpp的new之后,也就是在Bpp new自己的时候,Cpp还是null,所以会抛出NPE;但是在情况1中,由于判断条件为false,并没有跑到使用CPP的地方,所以不会抛出NPE。

正确做法:把new Bpp放到所有static块的最后

 1 package com.paypal.dinyu;
 2 
 3 /**
 4  * Created by dinyu on 26/07/2017.
 5  */
 6 public class Bpp {
 7     private static final Cpp CPP = Cpp.getInstance();
 8     private static final Bpp INSTANCE = new Bpp();
 9 
10     private Bpp() {
11         init();
12     }
13 
14     public static Bpp getInstance() {
15         return INSTANCE;
16     }
17 
18     private void init() {
19         if (true) {
20             System.out.println("CPP.toString: " + CPP.toString());
21         }
22     }
23 
24     public static void main(String[] args) {
25         Bpp.getInstance();
26     }
27 }
28 
29 class Cpp {
30     private static final Cpp INSTANCE = new Cpp();
31 
32     private Cpp() {
33 
34     }
35 
36     public static Cpp getInstance() {
37         return INSTANCE;
38     }
39 }

输出结果:

CPP.toString: com.paypal.dinyu.Cpp@63947c6b

Process finished with exit code 0

 

转载于:https://www.cnblogs.com/ydjfish/p/7237995.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值