Wednesday, September 20, 2006

Final Methods and Performance

When compiling code, each method call to a final method can be replaced by the actual method code. This is known as inlining. Inlining can potentially speed up program execution as final methods are inlined at compile time. A final method can be optimized in this way because the compiler knows it will not have to look up the correct version of the method at runtime, as polymorphism does not exist for final methods.

The latest virtual machines, however, can detect whether or not a non-final method is overridden. So declaring methods final to improve performance is no longer as valid as it once was.

No comments: