DEV Community

MANIKANDAN
MANIKANDAN

Posted on

The “final” Keyword in Java

Overview

We will take a look at what the final keyword is, why it is used, where it is used and how to use it

Final
->In Java, the final keyword is used to denote constants. It can be used with variables, methods, and classes.

Using the final keyword in Java is like making a promise to yourself and other programmers that something won't be changed. You use it to enforce rules and prevent accidental modifications. It's all about making your code safer and more predictable.

The final keyword in Java is a non-access modifier used to prevent modification.

So, what is a non-access modifier?

Before going to that, we have to understand what an access modifier is

Access modifier
Access modifiers are like the locks on the doors. They control who can get inside and see what's there (public, private, protected ).

Non-access modifier
These are like special instructions or rules for how the house is built and how it behaves. They don't control who can enter, but they change the house's nature(Static, final)

Now you may wonder why you should use 'final'?

It is used to restrict the user from further modifying the entity to which it is applied. This keyword plays a crucial role in ensuring immutability and preventing inheritance or method overriding.

After knowing what and why the final is, you should know where to use it

We can use it in classmethodvariable

1) Final class

--->In Java, the final class cannot be inherited by another class. For example

final class FinalClass {
  public void display() {
    System.out.println("This is a final method.");
  }
}

// try to extend the final class
class Main extends FinalClass {
  public  void display() {
    System.out.println("The final method is overridden.");
  }

  public static void main(String[] args) {
    Main obj = new Main();
    obj.display();
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a final class named FinalClass. Here, we have tried to inherit the final class by the Main class.

When we run the program, we will get a compilation error with the following message.

cannot inherit from final FinalClass
class Main extends FinalClass {
Enter fullscreen mode Exit fullscreen mode

2)final Method
In Java, the final method cannot be overridden by the child class. For example

class FinalDemo {

    public final void display() {
      System.out.println("This is a final method.");
    }
}

class Main extends FinalDemo {

  public final void display() {
    System.out.println("The final method is overridden.");
  }

  public static void main(String[] args) {
    Main obj = new Main();
    obj.display();
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a final method named display() inside the FinalDemo class. Here, the Main class inherits the FinalDemo class.

We have tried to override the final method in the Main class. When we run the program, we will get a compilation error with the following message.

display() in Main cannot override display() in FinalDemo
  public final void display() {
                    ^
  overridden method is final
Enter fullscreen mode Exit fullscreen mode

3) final Variable

In Java, we cannot change the value of a final variable. For example

class Main {
  public static void main(String[] args) {


    final int AGE = 32;
    AGE = 45;
    System.out.println("Age: " + AGE);
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above program, we have created a final variable named age. And we have tried to change the value of the final variable.

When we run the program, we will get a compilation error with the following message.

cannot assign a value to final variable AGE
    AGE = 45;
    ^
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ranjith_ranjith_6851c8e0a profile image
Ranjith Ranjith

Nice bro