

When to make a method final in Java? Example

The final keyword in Java is not as mysterious as volatile or transient, but still, it creates a lot of doubts in programmers' minds. I often receive questions like, When to make a method final in Java or When to make a method static in Java, later I answered in my earlier post. Questions like this are not trivial, knowing what a keyword does is just a small part of mastering that functionality. Similar to the real world, where knowing that a sword can cut a lot of things is not enough for a warrior to survive, you need to know how to use that and more importantly, use it effectively. 

The final keyword can be applied to a class, methods, and variable and has a different meaning for each of them, but the motive remains the same, it states completeness, it opposes change. For example, the final class can not be extended, the value of a final variable cannot be changed and a final method can not be overridden in Java.

This gives you the first hint when to make a method final in Java, obviously to prevent sub class from changing its definition, technical preventing it from overriding. There are a lot of clues on How to use the final keyword on methods in Java API itself, one of the prime examples of this java.lang.Object class, which declares a lot of method final including wait method with a timeout.

Another example of making a method final is the template method, which outlines an algorithm in the Template method design pattern. Since, you don't want a subclass to change the outline of the algorithm, making it final will prevent any accidental or malicious overriding. In this tutorial, we will learn a few things, which will help you to effectively use final keywords with Java methods.

The final method in Java

One of the best practices which I learned from clean code is about good designs, which mandates and ensures things, rather than listing some guidelines. A prime example of this is the clone() method, whose correct implementation depends upon following some guidelines, which is not ensured by the system.

By making a Java method final, you not only indicate but also ensures that no one can change its definition. This is particularly important while dealing with sensitive method, which deals with security and critical functionalities.

The Template method pattern is a good example of when to make a method final. In the template method pattern, the outline of the method is captured in one method but hooks are provided to add flexibility in implementation.

By making the template method final, we ensure that steps defined in algorithms are exactly followed by each class, but gives them the flexibility to define steps in their own term.

By the way, in Java, making a class final, automatically ensures that all it's methods can not be overridden, which is equivalent to making the final. Making a class final is also an important step to create an Immutable class in Java.

Why make a method final in Java


1) Methods which are called from constructor should be final, otherwise, a subclass may override and alter the behavior of the method, which may result in surprise and undesirable behavior.

2) Consider making the performance-critical method final, this given compiler more opportunity to optimize it. The compiler may inline or cache such methods because they can not be changed during the application lifetime. 

Though modern JIT is even capable of the in-lining non-final method, if they think it can result in performance gain and it's not overridden, making a method final can do this in-lining even before JIT comes into the picture.



In short, making a method final is mainly driven by design and safety requirements. Though performance can also be considered one of the reasons, it is not the main reason in today's Java world of advanced JIT. It's also a good idea to document, why you made a method final, that will help develop, who will maintain your code. 

There is also another doubt about making a private method final or not. Since the private method can not be overridden, neither in a subclass, where it's not accessible nor in inner class where it's accessible, because they are bonded using static binding using the class name. Making a private method final doesn't provide any additional value.

That's all on this Java tutorial about when to make a method final in Java. The main reasoning behind making a method final is to prevent it from being overridden by subclasses. By making a method final, you not only indicate but also ensures that your tried and tested method is not overridden. As a best practice of prevention, any security-sensitive method related to authentication and authorization should be final. 

Similarly, critical methods, which define critical functionality like template method must be final. Last, but not least, any method which is called from constructor must be final to avoid any surprise, which may result due to accidental or malicious overriding in a subclass.


Anonymous said...


Anonymous said...


class SensitiveClass{

public perform(SensitiveAction e){
// do something


protected void doSecurityCheck() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new sesnsitivePermission("sesitiveAction"));



@Javin Nice article just want to add that..

Final keyword has a numerous way to use:

A final class cannot be subclassed.
A final method cannot be overridden by subclasses
A final variable can only be initialized once
Other usage:


Kabir said...


Anonymous said...





Anonymous said...

Just a heads up, you have a little grammar mistake in the 5th paragraph: "but also ensures that no one can it's definition"

javin paul said...

Thanks for heads=up, *Change* was missing there, added now.

Post a Comment