advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 2.6/5 | Rate this item | 21 users have rated this item.
Expertise: Intermediate
Language: Java
January 6, 2009
Using the Final Keyword in Java
Contrary to popular opinion, the compiler cannot safely inline methods that have been declared as final. This is because these methods may have non-final declarations at runtime.

For example, suppose the compiler looks at class A, subclass B, and sub-subclass C. It sees a final method in A which it inlines into C. But at runtime, the versions loaded for A and B are different—the method is not final in A and it's overridden in B. Finally, C uses the incorrectly inlined version. This is due to a bug in some early compilers with the -O option. Thus, tips suggesting using either final or the -O option are not valid.

Conversely, final methods can be inlined after you've loaded them into the JVM. Because at that point the JVM knows definitely that the method is final. Compilers that operate after class loading, such as JIT compilers, are able to take advantage of final methods. Consequently, methods declared as final may have some performance benefit.

Generally, don't go out of your way to declare a method or class final purely for performance reasons. Only after you've definitely identified a performance problem is this even worth considering.

On the other hand, final variables, especially static final variables, are well worth using as standard.

Ashu Passi
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs