Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-final methods in a final class

My question is pretty simple:
Does the compiler treat all the methods in a final class as being final themselves? Does adding the final keyword to methods in a final class has any effect?

I understood that final methods have a better chance of getting inlined and this is why I am asking.

Thanks in advance.

like image 835
Acidic Avatar asked Jan 07 '12 01:01

Acidic


People also ask

Can final class have non final methods?

All methods in a final class are implicitly final. To be pedantic, whether or not the methods are implicitly final is moot; there is no opportunity to attempt to override them!

Are all methods in a final class final?

No. Making a class final and making its variables and methods final are different things. Re your fairly significant edit: Can we achieve this immutability requirement by making all methods of class as final and declaring all variables as private, to achieve this condition for immutability?

Can final class have public methods?

In java final variables can't reassign, final classes can't extends and final methods can't override.

What is the difference between final class and final method?

If we initialize a variable with the final keyword, then we cannot modify its value. If we declare a method as final, then it cannot be overridden by any subclasses. And, if we declare a class as final, we restrict the other classes to inherit or extend it.


2 Answers

You're correct, all methods in a final class are implicitly final.

See here:

"Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class."

And here:

All methods in a final class are implicitly final.

This may also be of interest for you: Performance tips for the Java final keyword

like image 162
EboMike Avatar answered Sep 30 '22 02:09

EboMike


Does the compiler treat all the methods in a final class as being final themselves?

In effect, yes it does. A method in a final class cannot be overridden. Adding (or removing) a final keyword to a method makes no difference to this rule.

Does adding the final keyword to methods in a final class has any effect?

In practice, it has minimal effect. It has no effect on the rules on overriding (see above), and no effect on inlining (see below).

It is possible to tell at runtime if a method was declared with a final keyword ... using reflection to look at the method's flags. So it does have some effect, albeit an effect that it irrelevant to 99.99% of programs.

I understood that final methods have a better chance of getting inlined and this is why I am asking.

This understanding is incorrect. The JIT compiler in a modern JVMs keeps track of which methods are not overridden in the classes loaded by an application. It uses this information, and the static types to determine whether a particular call requires virtual class dispatching or not. If not, then inlining is possible, and will be used depending on how large the method body is. In effect, the JIT compiler ignores the presence / absence of final, and uses a more accurate method to detect method calls where inlining of the method is allowable.

(In fact it is more complex than this. An application can dynamically load subclasses that cause the JIT compiler's method override analysis to become incorrect. If this happens, the JVM needs to invalidate any effected compiled methods and cause them to be recompiled.)


The bottom line is:

  • There is NO performance advantage in adding final to methods in final classes.

  • There might be a performance advantage in final to methods in non-final classes, but only if you are using an old Sun JVM, or some other Java / Java-like platform with a poor quality JIT compiler.

If you care about performance, it is better to use an up-to-date / high performance Java platform with a decent JIT compiler than to pollute your code-base with final keywords that are liable to cause you problems in the future.


You wrote in a comment:

@RussellZahniser I have read differently in many places.

The internet is full of old information, much of which is out of date ... or was never correct in the first place.

like image 26
Stephen C Avatar answered Sep 30 '22 03:09

Stephen C