Back to: Java Tutorials
Introduction
The Java final keyword in Java can be used in various contexts to define constants, methods, and classes. Learn more about the uses of the final keyword.
The final
keyword is used to declare a constant value or to prevent a class, method, or variable from being modified. When a variable or object is declared as final
, its value cannot be changed throughout the program’s execution. This keyword provides a way to create immutable variables and objects in Java.
Java Final variables
A final variable is a variable whose value cannot be changed once it has been initialized. Final variables are typically used to define constants in a program. In Java, we can declare a final variable using the final
keyword.
Java Final Variable Code Example
Here is an example of declaring a final variable:
/**
* Final variable example
*
* @author Mamun Kayum
*
*/
public class FinalKeywordEx {
final int MAX_VALUE = 100;
public static void main(String[] args) {
FinalKeywordEx ex = new FinalKeywordEx();
ex.printMaxVal();
}
void printMaxVal() {
System.out.println("max value: " + MAX_VALUE);
// MAX_VALUE = 10; // this will an compilation error "The final field
// FinalKeywordEx.MAX_VALUE cannot be assigned"
}
}
In the above example, the value of MAX_VALUE
cannot be changed throughout the program’s execution.
Java Final methods
A final method is a method that cannot be overridden by a subclass. This is useful when we want to prevent a subclass from changing the behavior of a method. To declare a method as final, we can use the final
keyword before the method declaration.
Here are some scenarios when it is appropriate to use final methods in Java:
- Security: If you have a method that performs a critical security check, you might want to make it final to ensure that its behavior cannot be modified by any subclass.
- Efficiency: Final methods can be inlined by the compiler, which can result in faster execution times. If you have a method that is called frequently and has a fixed behavior, making it final can improve performance.
- Consistency: If you have a method that is critical to the overall behavior of a class, you might want to make it final to ensure that its behavior is consistent across all subclasses.
- Design: If you have a well-defined class hierarchy and you want to prevent subclasses from modifying the behavior of certain methods, making those methods final can help enforce your design.
Final Method Code Example
Here is an example of a final method:
public class Parent {
public final void printMessage() {
System.out.println("This is a final method.");
}
}
public class Child extends Parent {
// This will give a compile-time error
public void printMessage() {
System.out.println("This method cannot be overridden.");
}
}
In the above example, the printMessage()
method in the Parent
class is declared as final. When we try to override this method in the Child
class, the compiler will give a compile-time error.
Java Final Class
A final class in Java is a class that cannot be subclassed or extended. To create a final class, you simply add the “final” keyword before the class definition. Here’s an example:
final class MyClass {
// class definition
}
In this example, MyClass
is a final class that cannot be extended by any other class.
When to Use Final Classes
Final classes are often used for security purposes or to prevent unintended changes to critical functionality. For example, if you have a class that contains sensitive data, you might want to make it final to prevent anyone from subclassing it and accessing that data.
Another common use of final classes is in utility classes that provide helper methods or constants. Utility classes are typically made final to prevent accidental extension and to ensure that their functionality remains consistent.
Example of a Final Class
Here’s an example of a final class in Java:
final class MathUtils {
private MathUtils() {}
public static final double PI = 3.14159265358979323846;
public static double square(double x) {
return x * x;
}
public static double cube(double x) {
return x * x * x;
}
}
In this example, MathUtils
is a final class that contains two static methods for calculating the square and cube of a number, as well as a constant value for PI. The class also has a private constructor to prevent instantiation.
Because MathUtils
is a final class, it cannot be extended or modified by any other class. This ensures that its functionality remains consistent and predictable.
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author