java踩坑记-static final 顺序


这个坑是关于单例模式 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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM