Advantage to using keyword final?

Started by Fool Running, July 27, 2006, 23:53:47

Previous topic - Next topic

Fool Running

Is there any speed advantage to specifing a variable/class/method as final?
As in: is the compiler able to do some optimizations if something is declared final?

Just curious :lol:
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

spasi

Variables: I'm not sure if making a class field final allows for any optimizations. The final keyword is stripped from method parameters and local variables at compile time.

Classes: Making a class final (or private if it's a local class) is the same as making each one of its methods final.

Methods: A final method is equally optimizable to a non-final method that has not been overloaded (yet). So, you aren't losing anything if you don't specify final. Also, Mustang now has support for bimorphic call inlining, so you can still have up to two implementations of method and still not lose any performance.

I personally use final on every method parameter possible and usually on most local variables, even if it doesn't make a difference at runtime and isn't very clean code-wise. I find that this habit allows me to spot bugs very early (especially while I'm coding), distinguishes method input from method implementation and also makes the algorithm clearer to understand.

Fool Running

Thanks for the info :D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D