Final Keyword in Java – Learn to Implement with Methods & Classes

by DataFlair Team ·
Published June 16, 2018
· Updated September 7, 2019

Final Keyword in Java is used to finalize the value of a variable, class or method in the entire program. It stores the initialized value in the memory which will never change at any condition. There are much more to learn and implement the final keywords. So, let’s start with the introduction to Final Keywords.

What is Java Final Keyword?

Final keyword in Java is used in many contexts like it can be used as a variable, it can be a class or can be a method. Inheritance enables us to reuse the existing code but sometimes we want to restrict the extensibility; the final keyword allows us to do so. It is a non-access modifier that means if you make a variable or class final then you are not allowed to change that variable or class and if you try then the compiler will throw compilation error.

Implementation of Final Keywords

1. Java Final Variable

We should initialize a final variable in Java, otherwise, a compiler can throw a compile-time error. A Java final variable will only initialize once, either via an initializer or an assignment statement.

There are 3 ways to initialize a Java final variable:

You can initialize a final variable when it’s declared. This approach is the most common. A final variable is named blank final variable if it’s not initialized whereas declaration.

A blank final variable is initialized within instance-initializer block or within a constructor. If you have got one constructor in your class then it should initialize in all of them, otherwise, a compile-time error is thrown.

A blank final static variable is initialized within a static block.

1.1 When to use a Final Variable in Java?

The only difference between a normal variable and a final variable is that we will re-assign value to a normal variable. However, we cannot change the value of a final variable once assigned. Therefore, Java final variables must be used for the values that we would like to keep constant throughout the execution of a program.

Let us see above different ways of initializing a Java final variable through an example.

2. Java Final Class

A class which is declared using the final keyword is a final class. A final class can’t be extended. There are many final classes available in Java one of them is Java String class. If we try to inherit from final class then it will generate a compile-time error.

For example, All wrapper class in java like, float, integer etc. are Java final Class and we cannot extend them.

3. Java Final Method

When a method is declared in Java using the final keyword, then it’s called a final method. A final method can not be overridden. We should declare strategies with a final keyword in Java, that we tend to needed to follow constant implementation throughout all the derived classes. The subsequent fragment illustrates final keyword with a method:

Summary

We can use Final keyword in Java to restrict the user. It initializes the value once and can’t be changed during the action/entire process. Now, you practice this keyword with variables, methods, and classes.

If you face any doubt, feel free to share with us our experts will definitely get back to you!