Using ‘final’ Keyword in Java

In this post, you will learn to:

  • Describe final variables.
  • Describe final methods.
  • State the purpose of final classes.

‘final’ Variables

Many programming languages have a specific keyword to define constant identifiers or to hold values that are not likely to change during the execution of the program. In Java, the final keyword is used with variables to indicate that they are constant identifiers. Constant variables are declared with the final keyword and are assigned a value at the time of declaration. A compile time error is raised if a final variable is reassigned a value in a program after its initial declaration. The final variables indicate that the values will not change anytime later.

The following is the syntax for declaring final variable.

public final <data type> <variable name>= <value>;

The following code demonstrates the declaration of final variable.

public final int MAX_COLS = 100;

Here, a variable named MAX_COLS is declared as final and initialized with a value of 100.

‘final’ Methods

To prevent a method from being overridden or hidden in a Java subclass, it is declared as final. A method should be declared as final if a change in implementation affects the consistent state of the object. Methods that are declared private or part of the final class are implicitly final. The final methods can be invoked just like any other method. The final methods cannot be declared as abstract.

The following is the syntax for declaring final method.

<access specifier> final <return type> method_name([parameters]) {
   ...
}

The following code demonstrates the declaration of final method.

public class Proposal {
    final void finalMemo() {
       System.out.println("This is the final memo");
    }
}

Here, finalMemo() is a final method and cannot be overridden or hidden by any other method.

Final Classes

A class that cannot be subclassed is called a final class in Java. The final keyword is applied to the class declaration to achieve this. A primary reason why classes may be declared as final would to be to limit extensibility and to prevent the modification of the class definitions. Once a class is defined as final, other classes cannot be derived from it, and hence cannot be modified. A final class may or may not have final methods. It is not necessary for a class having a final method to be declared as final. The final classes can be instantiated, that is, a final class cannot have any instances of its own.

The following is the syntax for declaring final class.

<access specifier> final class <class name>{ }

The following code demonstrates the declaration of final class, Specifications.
Programmer A defines a class as follows:

final class Specifications{
   public void beginText(){
      System.out.println("Beginning Text of the specifications"); 
   }
   public void bodyText(String text){
      System.out.println(text);
   }
   public void endText(){
      System.out.println("Ending Text of the specifications"); 
   }
}

The following code demonstrates the initialization of final class, Specifications.

Programmer B defines another class which makes use of the earlier class:

public class MainDisplay {
   public static void main(String args[]) {
      Specifications objSpec = new Specifications();
      System.out.println("Enter text for the specifications"); 
      Scanner scan = new Scanner(System.in);
      String text = scan.next();
      objSpec.beginText();
      objSpec.bodyText(text);
      objSpec.EndText();
   }
}

Here, Programmer B cannot modify the class, Specifications in any way but can invoke its methods. Thus, the class defined by Programmer A acts like a read-only class.

Related Post