final Keyword in Java

Updated:

By

The final keyword in Java is a non-access modifier that can be used with variables, methods, and classes. It prevents the value of a variable, the behavior of a method, or the structure of a class from being changed.

Use case of final keyword in Java

The final keyword can be used in Java in three ways:

  • To declare constants. A final variable can only be assigned once, and its value cannot be changed after that. This makes it useful for declaring constants, such as PI or the number of seconds in a minute.
  • To prevent methods from being overridden. A final method cannot be overridden by a subclass. This is useful for methods that are part of a class’s public API and should not be modified by subclasses.
  • To prevent classes from being inherited from. A final class cannot be inherited from by a subclass. This is useful for classes that should not be extended, such as the String class.

Why use final keyword in Java?

There are several reasons why we might want to use the final keyword in Java:

  • To improve code readability and maintainability. The final keyword can make our code more readable and maintainable by making it clear which variables and methods cannot be changed.
  • To prevent errors. The final keyword can help to prevent errors by preventing variables and methods from being changed in ways that could cause problems.
  • To enforce design decisions. The final keyword can be used to enforce design decisions, such as ensuring that a class cannot be extended or that a method cannot be overridden.

The final keyword and constants

To declare a final variable, we simply add the final keyword to the variable declaration.

For example:

final int MAX_VALUE = 100;

This declares a final variable named MAX_VALUE that is initialized to the value 100 and it cannot be changed after it is initialized.

A constant is a value that is fixed. We can create constants in Java using the final keyword. In the above example, the MAX_VALUE is also a constant.

The final keyword and methods

To declare a final method, we add the final keyword to the method declaration.

For example:

final void printHello() {
  System.out.println("Hello!");
}

This declares a final method named printHello() that prints the message “Hello!”. The printHello() method cannot be overridden by a subclass. If we try to override this method the compiler will show an error.

The final keyword and classes

To declare a final class, we add the final keyword to the class declaration.

For example:

final class Point {
  int x;
  int y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

This declares a final class named Point that has two fields, x and y. The Point class cannot be inherited from by a subclass.

When a class is declared as final, it cannot be subclassed. This means that no other class can inherit from the final class.

Summary of the key points

  • The final keyword in Java is a non-access modifier that can be used with variables, methods, and classes.
  • The final keyword can be used to declare constants, prevent methods from being overridden, and prevent classes from being inherited from.
  • There are several reasons why we might want to use the final keyword in Java, including improving code readability and maintainability, preventing errors, and enforcing design decisions.

FAQs

When should I use the final keyword for variables?

You should use final keyword in Java to apply restrictions on classes, methods, and variables. When applied to a class, it makes the class unable to be extended. When used with a method, it prevents the method from being overridden. And when used with a variable, it makes the variable a constant that cannot be reassigned after initialization.

Can a final method be overloaded in Java?

Yes, we can overload a final method in Java. Overloading means defining multiple methods in the same class with different parameter lists. The final keyword only prevents the method from being overridden in derived classes. But it doesn’t restrict us from defining multiple methods with the same name but different parameters in the same class.

Is it possible to change the value of a final variable in Java?

No, once a final variable is assigned a value, it cannot be changed or reassigned. Any attempt to modify the value of a final variable will result in a compilation error.

What’s the difference between final, finally, and finalize in Java?

final is a keyword used to make a class, method, or variable unchangeable or non-extendable.

finally is a keyword used in exception handling to specify a block of code that will always be executed whether an exception occurs or not.

finalize is a method in the Object class that can be overridden to perform cleanup operations on an object before it’s garbage collected.

These three keywords serve different purposes in Java and are not related to each other in functionality.