java

Java Final Keyword


What is final keyword?

In Java, the final keyword is a modifier that can be applied to classes, methods, and variables to indicate that they cannot be further modified or extended.

Note: 

1. It is used to provide restrictions.

2. Using the final keyword in these contexts helps improve code safety, maintainability, and can also provide performance benefits in certain situations.

             

In java, we can apply final keyword in three ways:-

 

  • Final Variables (Constants): 

When applied to a variable, the final keyword indicates that the variable's value cannot be changed once it has been assigned.

Syntax:

class MyClass {
    final int constantValue = 10;

   }
}

Example 1:

class FinalVariableExample {
    // Final variable (constant)
    final int MY_CONSTANT = 100;

    public static void main(String[] args) {
        FinalVariableExample example = new FinalVariableExample();

        // Accessing the final variable
        System.out.println("My constant value: " + example.MY_CONSTANT);

    }
}

 

Output:

My constant value: 100

 

Example 2:

class FinalVariableExample {
    // Final variable (constant)
    final int MY_CONSTANT = 100;

    public static void main(String[] args) {
        FinalVariableExample example = new FinalVariableExample();

        // Accessing the final variable
        System.out.println("My constant value: " + example.MY_CONSTANT);

        // Error: Cannot assign a new value to a final variable
        example.MY_CONSTANT = 200;
        System.out.println("My constant value: " + example.MY_CONSTANT);
    }
}

 

Output:

ERROR!
javac /tmp/pkxKIParqE/FinalVariableExample.java
/tmp/pkxKIParqE/FinalVariableExample.java:12: error: cannot assign a value to final variable MY_CONSTANT
example.MY_CONSTANT = 200;
^
1 error

 

  • Final Methods:

When applied to a method, the final keyword means that the method cannot be overridden by subclasses. This is commonly used for security, efficiency, and to ensure that the intended behavior of a method is not altered by subclasses.

Syntax:

class ParentClass {
    final void finalMethod() {
        // method implementation
    }
}

class ChildClass extends ParentClass {
    // Cannot override finalMethod in ChildClass
}

 

Example:

class ParentClass {
    // Final method
    final void displayMessage() {
        System.out.println("This is a final method in the ParentClass");
    }
}

class ChildClass extends ParentClass {
    // Error: Cannot override final method
    /* void displayMessage() {
          System.out.println("Trying to override the final method");
      } */

    void additionalMethod() {
        System.out.println("This is an additional method in the ChildClass");
    }
}

class FinalMethodExample {
    public static void main(String[] args) {
        ChildClass Obj = new ChildClass();

        // Calling the final method from the parent class
        Obj .displayMessage();

        // Calling the additional method from the child class
        Obj .additionalMethod();
    }
}

 

Output:

This is a final method in the ParentClass
This is an additional method in the ChildClass

 

  • Final Classes:

If a class is declared as final, it means that it cannot be sub-classed. In other words, we cannot create a new class that extends a final class.

Syntax:

final class FinalClass {
    // class members and methods
}

 

Example:

// Final class
final class FinalClassExample {
    // Some members and methods
    void displayMessage() {
        System.out.println("This is a final class.");
    }
}

// Error: Cannot inherit from final class
// class SubClass extends FinalClassExample {}

public class Main {
    public static void main(String[] args) {
        FinalClassExample finalObject = new FinalClassExample();

        // Accessing a method of the final class
        finalObject.displayMessage();
    }
}

 

Output:

This is a final class.