Performance of Java 'final' (ctd)

Following from our description of a test to investigate the relative timings of calls on instances of final vs non-final classes, we thus have six different test configurations. These comprise the combinations of category (non-final, final, extended) and whether or not the precise subclass is known to the compiler. Figure 1 below shows the results of this test (mean timing to make a million calls in each of these six configurations).

Graph: final vs non-final, known vs unknown subclass

Figure 1: relative timings of method calls on classes that are non-final (and not extended), final and non-final but extended by another class, in cases where the precise subclass is known or not to the compiler. Each timing is actually the mean of 50 runs reported by System.nanoTime(), under Hotspo 1.6.0 64-bit running on an Intel i7 under Windows 7.

What we observe is that specifying a class as final makes no performance difference. There is no evidence of any supposed optimisation that the JVM can make based on whether or not a class is final (or indeed because there exist objects of another class extending the class in question).

Irrespectively of whether or not the class is final, there is a potential optimisation if the precise subclass is known.