Java static final values replaced in code when compiling?

后端 未结 10 1318
死守一世寂寞
死守一世寂寞 2020-11-28 10:27

In java, say I have the following

==fileA.java==
class A
{  
    public static final int SIZE = 100;
}  

Then in another file I use this valu

10条回答
  •  余生分开走
    2020-11-28 11:21

    The important concept here is that the static final field is initialised with a compile-time constant, as defined in the JLS. Use a non-constant initialiser (or non-static or non-final) and it wont be copied:

    public static final int SIZE = null!=null?0: 100;
    

    (null is not a *compile-time constant`.)

提交回复
热议问题