Skip to main content Skip to docs navigation
TechSpiderTutorials

Java final Keyword

final modifier

A variable can be declared as final. Doing so prevents its contents from being modified. This means that you must initialize a final variable when it is declared.

It is a common coding convention to choose all uppercase identifiers for final variables. Variables declared as final do not occupy memory on a per-instance basis. Thus, a final variable is essentially a constant.

The keyword final can also be applied to methods, but its meaning is substantially different than when it is applied to variables.

Final Variables: Constants and Immutability

int number=6000;
number=5000;

final float PI=3.14;
PI=4.14; //error

Using final keyword with Methods

You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this-a number of its methods are final.

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. For example, you might want to make the getFirstPlayer method in this ChessAlgorithm class final:

class ChessAlgorithm
 {
    enum ChessPlayer { WHITE, BLACK }
    ...
    final ChessPlayer getFirstPlayer()
     {
        return ChessPlayer.WHITE;
    }
    ...
}

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.

Using final with classes

You can 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.

public final class UtilityClass {
    // Class members and methods
}

Using final with Inheritance

The keyword final has three uses. First, it can be used to create the equivalent of a named constant. The other two uses of final apply to inheritance.

Preventing Method Overriding

To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden. The following fragment illustrates final:
class ClaassA
  {
          final void method() 
          {
          System.out.println("This is a final method.");
         }
  }
  class ClaassB extends ClaassA
   {
          void method()
      { // ERROR! Can't override.
          System.out.println("Illegal!");
      }
    }

Because method( ) is declared as final, it cannot be overridden in ClaassB. If you attempt to do so, a compile-time error will result.

Preventing Class Inheritance

Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final, too. As you might expect, it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations. Here is an example of a final class:
final class ClassA 
{
          // ...
}
          // The following class is illegal.
          
class ClassB extends ClassA { // ERROR! Can't subclass ClassA
          // ...
          }
					

As the comments imply, it is illegal for ClassB to inherit ClassA since ClassA is declared as final.

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

static final double PI = 3.141592653589793;

Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so. By convention, the name of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated by an underscore (_).