Java - Final Modifier


Final is modifier prohibiting value modification.



Declaring an entire class final prevents the class from being subclassed.

This is particularly useful, for example, when creating an immutable class like the String class.

An interface cannot be final


You use the final modifier in a method declaration to indicate that the method cannot be overridden by subclasses.

You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object.

Example of using a final method for initializing an instance variable:

class Whatever {
    private varType myVar = initializeInstanceVariable();
    protected final varType initializeInstanceVariable() {

        //initialization code goes here

This is especially useful if subclasses might want to reuse the initialization method. The method is final because calling non-final methods during instance initialization can cause problems.

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.


The modifier final indicate that the value of a member will never change

The final modifier indicates that the value of a field cannot change.

The reference to final variable is thread safe since it can never be changed.

A final member can be initialized through:

  • an Initializer expression
final int myFinalValue = 5;
final int myFinalValue = myFunction();

protected final int  initializeInstanceVariable() {

        // initialization code goes here
        myFinalValue = 5;

Powered by ComboStrap