final Keyword in Java
The final
keyword in Java is a non-access modifier that can be applied to variables, methods, and classes. It is used to restrict the user from further modifying the entity to which it is applied. This keyword plays a crucial role in ensuring immutability and preventing inheritance or method overriding.
Usage
1. final
Variables
A final
variable is a constant; once initialized, its value cannot be changed.
final int MAX_VALUE = 100;
Initialization of final
Variables
A final
variable must be initialized when it is declared or within a constructor if it is an instance variable.
public class FinalInitialization {
final int MAX_VALUE;
public FinalInitialization() {
MAX_VALUE = 100;
}
}
2. final
Methods
A final
method cannot be overridden by subclasses, ensuring that the method's implementation remains unchanged.
class Parent {
public final void display() {
System.out.println("This is a final method.");
}
}
3. final
Classes
A final
class cannot be subclassed, preventing inheritance.
public final class FinalClass {
// Class implementation
}
Examples
Example 1: final
Variable
public class FinalVariableExample {
public static void main(String[] args) {
final int MAX_VALUE = 100;
System.out.println("MAX_VALUE: " + MAX_VALUE);
// MAX_VALUE = 200; // This will cause a compilation error
}
}
In this example, MAX_VALUE
is a final
variable. Attempting to reassign it will result in a compilation error.
Example 2: final
Method
class Parent {
public final void display() {
System.out.println("This is a final method.");
}
}
class Child extends Parent {
// public void display() { // This will cause a compilation error
// System.out.println("Attempting to override.");
// }
}
public class FinalMethodExample {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Here, the display
method in the Parent
class is marked as final
, preventing it from being overridden in the Child
class.
Example 3: final
Class
public final class FinalClass {
public void show() {
System.out.println("This is a final class.");
}
}
// class SubClass extends FinalClass { // This will cause a compilation error
// }
public class FinalClassExample {
public static void main(String[] args) {
FinalClass obj = new FinalClass();
obj.show();
}
}
In this example, FinalClass
is declared as final
, which means it cannot be extended by any other class.
Tips and Best Practices
- Immutability: Use
final
variables to create constants and ensure that their values remain unchanged throughout the program. - Security: Use
final
methods to prevent subclasses from altering critical methods, which can help in maintaining security and consistency. - Design: Use
final
classes to prevent inheritance when designing immutable classes or when you want to ensure that the class's implementation cannot be altered. - Static Final Variables: Unlike instance final variables, static final variables cannot be assigned via constructors. They are typically initialized in their declaration.
public class FinalStaticExample { public static final double PI = 3.14159; }
- Performance: In some cases, the use of
final
can help the Java compiler and JIT compiler to optimize the code, as it provides more information about the invariability of the value.