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?
I presume many structural/logical reasons might come here, but speaking from the performance point of view? Does it matter?
Thanks,
In a modern JVM, final shouldn’t affect performance. This is especially true for private fields, but even for non-private fields the JIT can optimize non-final fields as thought they are final, and then deoptimize if it loads some code that actually does modify the field.
That said, the main reason to use final is not performance, but to make your code more maintainable. By making fields final you are reducing the number of “moving parts” readers of your code have to think about, making it much easier to reason about the code.