Yin99 said:
I read somewhere that using the final keyword on a method (and/or
variable) can make the code more efficient because the compiler can
'inline' the final compiled version (I'm shaky on technical details.)
Is there any truth to this?
There might be some truth to this, depending on the characteristics of your
program and the particular version of the particular JVM you're using. Some
JVMs have an abundance of command-line arguments that might affect behavior
also.
A final method is somewhat easier for the JVM to inline than a non-final
method, but that does not mean that non-final methods won't be inlined, nor
does it mean that final methods will be.
So, in general: use the 'final' keyword for its semantic meanings:
A) On a class, it means that the class cannot be subclassed. (And
implicitly, that its methods are final.)
B) On a method, it means that the method cannot be overridden.
C) On a variable, it means that the variable's value cannot change (once the
variable has been initialized).
Use the 'final' keyword for performance reasons if and only if you can prove
that it actually improves performance in your particular set of
circumstances.
-- Adam Maass