When I'm using lambda expressions or anonymous inner classes, with variables from outer class, I often get the compile time error:
Lamba expressions:
local variables referenced from a lambda expression must be final or effectively final
Inner classes:
local variables referenced from an inner class must be final or effectively final
It means that compiler in Java 8 is able to deduce whether variable is implicitly final or not.
Regarding to this question, using final variables instead of non-final sometimes gives a huge positive impact on performance.
My question is:
Does compiler in java 8 interpret effectively final variables as final variables and later, in runtime use it as final?
In consequence, does it make the same optimization, as it's doing for the final variables?
Questions about differences between effective final and final (i.e. this) are connected with the reason why it has to be effectively final but doesn't say anything which answers my question.
I would be grateful for any answers.
Does compiler in java 8 interpret effectively final variables as final variables and later, in runtime use it as final?
The answer will be yes in both cases.
The reason for the latter is that the class file format does not provide a way to say whether a local variable is declared as final
. Therefore, if the JIT compiler is going to optimize based on finality, the finality must be inferred from what the bytecodes of a method actually do; i.e. effective finality.