Should I declare a java field 'final' when if it's not modified in code?

前端 未结 7 2028
终归单人心
终归单人心 2020-12-15 21:06

My question is mainly about performance. The compiler knows better that, for example, some variable is NOT modified after object instantiation. So, why bother with final?

7条回答
  •  温柔的废话
    2020-12-15 21:19

    Using final for a field also has implications for thread safety. The Java Memory Model states that the values of final fields becomes visible to all threads that can access the object as soon as the constructor of that object finishes. That guarantee is similar to volatile fields, where any thread always sees the current value. There is no such guarantee for normal fields.

    And, as others noted, the JIT can perform aggressive optimizations for final fields, which are not so easy for normal fields, where any newly loaded class could have write accesses to the field.

提交回复
热议问题