4

I know that one of the uses of the final keyword on a method is forcing subclasses to use the same method implementation. But what does it mean to say that it increases efficiency by allowing the compiler to turn calls to the method into inline java code? I just read this but don't quite understand the idea.

To this extent, I don't understand the meaning of an inline Java code. How is a call to a final method executed? Is there anything special that Java notifies or does during compilation of code that calls a final method?

asteri
  • 11,402
  • 13
  • 60
  • 84
LeandreM
  • 943
  • 3
  • 12
  • 24

2 Answers2

5

The bytecodes are not significantly more or less efficient if you use final because Java bytecode compilers typically do little in the way optimization. The efficiency bonus (if any) will be in the native code produced by the JIT compiler.

In theory, using the final provides a hint to the JIT compiler that should help it optimize. In practice, recent HotSpot JIT compilers can do a better job by ignoring your hints. For instance, a modern JIT compiler typically performs a global analysis to find out if a given method call is a call to a leaf method in the context of the application's currently loaded classes. This analysis is more accurate than your final hints can be, and the runtime can even detect when a new class is loaded that invalidates the analysis ... and redo the analysis and native code generation for the affected code.

So best practice is to use final to (broadly speaking) express your design intentions, and to achieve other semantic effects that you require. (For instance using the final modifier can play an important role in implementing thread-safe immutable types.) If you use final as an optimization hint, you won't achieve much, and you will make your code harder to modify and extend.

font: link

Community
  • 1
  • 1
  • I agree but wouldn’t go so far as to say that the JIT’s analysis is *more* accurate than the final “hints” of a programmer as declaring something final is more than a hint, it’s mandatory and therefore as accurate as it ever can be. But in most cases the optimizer will detect final behavior and treat it the same way as final declarations. – Holger Aug 26 '13 at 10:07
1

When you want to know how the Java compiler handles different constructs, one good way to find out is to use javap to disassemble the class file.

Final methods may offer an opportunity for optimization because the code that will run is known at compile time. Otherwise, a lookup must be performed at run time, to determine which version (original or overridden) to run.

This is the theory... in practice, existing Java compilers don't do very aggressive optimization.

theglauber
  • 28,367
  • 7
  • 29
  • 47
  • 1
    As Leonardo Pugliese pointed out above, while the Java-to-bytecode compilers don't do much optimization, the bytecode-to-machinecode compiler in modern JVMs _do_ optimize aggressively, including this feature. – yshavit Aug 23 '13 at 14:44