LWJGL Forum

Programming => General Java Game Development => Topic started by: Fool Running on July 27, 2006, 23:53:47

Title: Advantage to using keyword final?
Post by: Fool Running on July 27, 2006, 23:53:47
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:
Title: Advantage to using keyword final?
Post by: spasi on July 28, 2006, 10:56:57
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.
Title: hmmmmmmm...
Post by: Fool Running on July 29, 2006, 18:25:49
Thanks for the info :D