Modifying final fields in Java

前端 未结 5 2010
故里飘歌
故里飘歌 2020-12-13 00:07

Let\'s start with a simple test case:

import java.lang.reflect.Field;

public class Test {
  private final int primitiveInt = 42;
  private final Integer wra         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 00:31

    Compile-time constants are inlined (at javac compile-time). See the JLS, in particular 15.28 defines a constant expression and 13.4.9 discusses binary compatibility or final fields and constants.

    If you make the field non-final or assign a non-compile time constant, the value is not inlined. For instance:

    private final String stringValue = null!=null?"": "42";

提交回复
热议问题