The final keyword in Java is used to restrict the user. It is also known as a non-access modifier. We can use the final keyword with:
When a variable is declared as final, it is known as a final variable. Its value cannot be changed once initialized. It behaves like a constant.
Here is the syntax to declare a final variable:
In the following code, the Main class has a final variable named SPEED_LIMIT. When we try to update the value of final variable SPEED_LIMIT to 400 by invoking the run() method, value does not change because final variable once initialized cannot be changed.
The following example demonstrates that a final variable cannot be modified once it is assigned a value.
When we compile the above code, it shows a compile-time error, as follows:
error: cannot assign a value to final variable SPEED_LIMIT
A method declared as final is known as a final method. Subclasses cannot override the final method.
Here is the syntax to declare a final method:
The following example demonstrates that a final method cannot be overridden in a subclass.
When we compile the above code, it shows a compile-time error, as follows:
Main.java:7: error: run() in Main cannot override run() in Bike
void run(){System.out.println("running safely with 100kmph");
^
overridden method is final
1 error
A class declared with the final keyword is known as a final class. Note that the final class cannot be inherited.
Here is the syntax to declare a final class:
The following example demonstrates that a final class cannot be inherited by another class.
When we compile the above code, it shows a compile-time error, as follows:
Main.java:3: error: cannot inherit from final Bike
public class Main extends Bike
^
1 error
A final method can be inherited by a child class, but it cannot be overridden. This is useful when you want to keep method implementation remains unchanged in subclasses.
The following example demonstrates inheriting a final method:
Output:
running...
A blank final variable is a final variable that is not initialized at the time of declaration. Once initialized, its value cannot be changed. These variables are useful when the value must be set during object creation, such as an employee’s PAN card number.
The following code snippet showing the use of blank or uninitialized final variable:
A blank final variable can be initialized only inside a constructor.
The following example demonstrates the initialization of a blank final variable:
Output:
70
A static blank final variable is a static final variable that is not initialized at the time of declaration. It can be initialized only inside a static block.
The following example demonstrates the use of static blank final variable:
Output:
50
A final parameter is a method parameter whose value cannot be changed inside the method.
The following example demonstrates the use of final parameter:
Output:
500
Here are the key advantages of using the final Keyword in Java, which enhance the security, performance, and maintainability of our code:
A constructor cannot be declared as final because constructors are never inherited, and the final keyword is mainly used to restrict inheritance and overriding.
There are several characteristics of the final keyword in Java. Some of them are as follows:
We request you to subscribe our newsletter for upcoming updates.