final Keyword in Java

Alexandru Nastase
3 min readSep 26, 2022

--

why, when and how to use it

Modifiers are specific keywords present in Java using that we can make changes to the characteristics of a variable, method, or class and limit its scope. Java programming language has a rich set of Modifiers.

Modifiers in Java are divided into two types Access Modifiers and Non-Access modifiers.

Non-Access modifiers

Non-access modifiers provide information about the characteristics of a class, method, or variable to the JVM. Seven types of Non-Access modifiers are present in Java. They are –

  1. final
  2. static
  3. abstract
  4. synchronized
  5. volatile
  6. transient
  7. native

This article will go into the details of the final keyword. Let’s explore first, why do we use it, when should we use it if we decide to, and last but not least, how to use it.

the WHY:

The final keyword in Java can be used for different purposes. Most commonly, it is used to create immutable objects. This means that once an object is created, it cannot be modified. This can be useful for creating objects that need to be thread-safe or when you want to ensure that data is not modified accidentally.

final also has uses beyond creating immutable objects. It can also be used to prevent inheritance or to make a class static. In short, the final keyword is a versatile tool that can be used in a variety of ways. While it is not always necessary to use final, it can be helpful in certain situations.

the WHEN and HOW:

1. Java final Variable

A variable cannot have its value modified after initialization if it is defined using the final keyword. Keep in mind that initializing a variable is not always required at the moment of declaration.

In the above gist, we tried to declare a final variable CLAPS to an initial value of 100, then when we tried to change it, we will see we get a compilation error with the following message.

2. Java final Method

A method, declared with the final keyword, cannot​ be overridden or hidden by subclasses.

In the above gist, we tried overriding the randomMethod which is a final method, inside another class.

We will see that when we try to run the code, we will get, again, a compilation error.

3. Java final Class

A class marked as final can extend another class, however a final class cannot be extended.

In the above gist, 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.

4. Java final Parameter

If you ever see the final keyword with a parameter variable, it means that the value of this variable cannot be changed anywhere in the method.

In the above gist, we tried to change the value of x, which is a final parameter.
If we run the code, we will see a compilation error saying that we cannot assign different values to final parameters.

Knowing best why, how and when to use final, as well as other modifiers, comes with experience, and experience comes with time.

Enjoy the journey and happy coding!

--

--

Alexandru Nastase

Java guy. Life newbie. Maybe smart maybe funny. Personal stuff here, too. WELCOME ! ❤