Contrary to the implication of many tips,
methods declared as
final cannot be safely inlined by the compiler,
because the method could have a non-
final declaration at runtime.
To see why, suppose the compiler looks at
class A and subclass B, and sub-subclass C and sees a
final method in A which
it inlines into C. But then at runtime the versions loaded for A and B are different
and the method is not
final in A, and overridden in B. Then C uses the
incorrectly inlined version. This was a bug in some of the early compilers when used with
the -O option. So tips suggesting using
final and the -O option are not valid tips.
On the other hand,
final methods can be inlined after being loaded
into the JVM, because at that point the JVM knows definitely that the method is
final. So compilers that operate after class loading, such as JIT compilers,
can take advantage of
final methods. Consequently, methods declared
final could have some performance benefit.
Generally, I wouldn't go out of my way to declare a method or class
purely for performance reasons. Only after you've definitely identified a performance
problem is this even worth considering.
On the other hand,
static final variables, are well worth using as standard.
The following pages have their detailed tips extracted below
The following detailed tips have been extracted from the raw tips page
Chapter 4 of "Java Performance Tuning", "Object Creation". (Page last updated September 2000, Added 2000-10-23, Author Jack Shirazi, Publisher O'Reilly). Tips:
finalmodifier on instance-variable definitions to create immutable internally accessible objects.
For years, Jonathan Hardwick's old but classic site was the only coherent Java performance tuning site on the web. He built it while doing his PhD. It wasn't updated beyond March 1998, when he moved to Microsoft, but most tips are still useful and valid. The URL is for the top page, there are another eight pages. Thanks Jonathan. (Page last updated March 1998, Added 2000-10-23, Author Jonathan Hardwick, Publisher Hardwick). Tips:
"Cutting Edge Java Game Programming". Oldish but still useful intro book to games programming using Java. (Page last updated 1996, Added 2001-06-18, Author Neil Bartlett, Steve Simkin , Publisher Coriolis). Tips:
Glen McCluskey's paper with 30 tuning tips, now free. (Page last updated October 1999, Added 2000-10-23, Author Glen McCluskey, Publisher McCluskey). Tips:
finalclasses can be faster.
Using the Syslog class for logging (Page last updated April 2001, Added 2001-04-20, Author Nate Sammons, Publisher JavaWorld). Tips:
static final booleans to wrap logging statements so that they can be easily turned off or eliminated.
http://www.bastie.de/resource/res/mjp.pdf and http://www.bastie.de/java/mjperformance/contents.html
Performance tuning report in German. Thanks to Peter Kofler for extracting the tips. (Page last updated November 2001, Added 2001-07-20, Author Sebastian Ritter, Publisher Ritter). Tips:
An assortment of tips (Page last updated 2000, Added 2000-10-23, Author Curt Smith, Publisher Smith). Tips:
Performance tuning (Page last updated September 2001, Added 2001-10-22, Author James McGovern, Publisher Java Developers Journal). Tips:
Both Java specific and Java on OS/390 tips. (Page last updated 2000, Added 2000-10-23, Author ?, Publisher IBM). Tips:
Tuning the IBM JVM and Linux (Page last updated May 2001, Added 2001-10-22, Authors Duc Vianney and James Phelan, Publisher IBM). Tips:
Peter Haggar's Practical Java Programming Language Guide. (Page last updated 2000, Added 2001-01-19, Author Peter Haggar, Publisher Addison-Wesley). Tips:
Finalizers (Page last updated March 2002, Added 2002-04-26, Author Phil Vickers, Publisher IBM). Tips:
Various tips. (Page last updated 1999?, Added 2000-10-23, Author Nate Sammons, Publisher Sammons). Tips:
Good Java practices, some of which are good for performance. (Page last updated January 2001, Added 2001-01-19, Author Dave Jarvis, Publisher JOOT). Tips:
IBM's list of Java performance tuning tips (same page, two URLs). (Page last updated 2000, Added 2000-10-23, Author ?, Publisher IBM). Tips:
Some IBM Java performance tips. Although intended for AS/400 Java, many tips are generally applicable (Page last updated ?, Added 2000-10-23, Author ?, Publisher IBM). Tips:
static finalwhen creating constants.