Hello Guest

Advantage to using keyword final?

  • 2 Replies
  • 5500 Views
Advantage to using keyword final?
« 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:
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

*

Offline spasi

  • *****
  • 2169
    • WebHotelier
Advantage to using keyword final?
« Reply #1 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.

hmmmmmmm...
« Reply #2 on: July 29, 2006, 18:25:49 »
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