Cannot change static final field using java reflection?

后端 未结 2 519
温柔的废话
温柔的废话 2021-01-02 11:16

I recently stumbled upon Change private static final field using Java reflection and tested polygenelubricants\' EverythingIsTrue class, works fine, Syste

2条回答
  •  粉色の甜心
    2021-01-02 11:43

    When accessing primitive static final fields, the Java compiler will assume that the value is a constant and inline the value instead of generating code that accesses the field. This means that the compiler will replace with the reference to the FALSE field with the value false. If you use reflection to access the field, you will see that the value of the field has actually changed.

    This will not work for non-primitive fields, as the value of an object reference can not be inlined at compile time.

提交回复
热议问题