Introduction
In this lesson of our Java course, we will learn about the Java final keyword. Java final keyword is used to create constants and make variables, methods, and classes unmodifiable. In this article, we’ll explore what the “final
” keyword is, how it works, and best practices for using it in Java code.
Table of Contents
1. What is the Final in Java?
In Java, the “final
” keyword is used to show that a class, method, or variable is immutable or cannot be changed. Declaring a variable as “final
” creates a constant that cannot be changed. Using the “final
” keyword with a method prohibits a subclass from overriding it. Finally, using the “final
” keyword with a class restricts it from being extended by a subclass.
The Java final keyword is often used to create constants, as it ensures that the value of the constant remains the same throughout the program. It can also ensure that certain methods or classes cannot be changed, which can help prevent bugs and improve code reliability. Let’s summarize some key aspects of Java final keyword.
- Final variable can’t be re-initialized , i.e. we can’t assign a different value to the variable defined as
final
. - A class declared as final using the final keyword can’t be extended.
- A method can’t be overridden if it’s defined as final.
2. Java Final Variables
To create a final variable in Java, the final
keyword is placed before the variable declaration. Once a final variable is assigned a value, it cannot be changed. Here’s an example:
final int MAX_VALUE = 100;
In this example, the variable “MAX_VALUE
” is declared as final and assigned the value of 100. This means that the value of “MAX_VALUE
” cannot be changed throughout the program. Final variables are often used for values that should not change throughout the program, such as constants or configuration settings.
class Main {
public static void main(String[] args) {
// create a final variable
final int MAX_VALUE = 10;
// try to change the final variable
MAX_VALUE = 100;
System.out.println("max value is: " + MAX_VALUE);
}
}
In the above program, we have created a final variable named MAX_VALUE. And we have tried to change the value of the final variable. If we try to run this program, we will get a compile time error.
3. Java Final Methods
The final
keyword is placed before the method declaration to mark it as the final method. Once a method is declared final, it cannot be overridden by a subclass. Here’s an example:
class Car {
public final void displayName(){
System.out.println("Welcome");
}
}
class BMW extends Car {
// try to override final method
public final void displayName(){
System.out.println("Welcome to BMW");
}
}
In this example, we have defined displayName() as the final
method. If we try to override the final method in the BMW class, we will get a compile time error with the following details:
'displayName()' cannot override 'displayName()' in 'Car'; overridden method is final
4. Java Final Classes
To create a final class in Java, the final
keyword is placed before the class declaration. Once a class is declared final, it cannot be extended by a subclass. Here’s an example:
final class Car {
public void displayName(){
System.out.println("Welcome");
}
}
class BMW extends Car {
// try to override final method
public void displayName(){
System.out.println("Welcome to BMW");
}
}
In this example, the “Car” class is declared as final using Java final keyword. This means that no subclass can extend the class and change its behavior. Final classes are often used to prevent classes from being changed or extended, which can help ensure code reliability and prevent bugs.If we try to compile the above code, we will see the following compilation error:
5. Best Practices for Using the “final” Keyword
If you are using the “final
” keyword in your Java code, there are some recommended practices to consider.
- Use final variables for values that should not change throughout the program, such as constants or configuration settings.
- Use final methods to prevent sub-classes from modifying behavior, which can help ensure code reliability and prevent bugs.
- Use final classes to prevent classes from being extended or changed, which can help ensure code reliability and prevent bugs.
- Only use the
final
keyword when necessary, as overuse can make code harder to maintain.
Developers can create Java code that is reliable, secure, and efficient by implementing these recommended techniques for using the “final
” keyword effectively.
Summary
The Java final keyword is an important feature that allows us to create constants and prevent variables, methods, and classes from being changed or extended. By using the “final” keyword, we can improve code reliability, prevent bugs, and make code more efficient. However, it’s important to use the “final” keyword only when necessary, as overuse can make code harder to maintain.As always, the source code for this article is available on our GitHub repository.