What is the use of the final keyword in Java?
Table of Contents
1. Short Answer
The final keyword in Java is used to restrict users from modifying variables, methods, or classes:
- Final Variables: Cannot be reassigned once initialized
- Final Methods: Cannot be overridden by subclasses
- Final Classes: Cannot be extended (inherited)
2. Detailed Explanation
The final keyword is a fundamental concept in Java that helps enforce immutability and prevent unwanted modifications. It's used in three main contexts: variables, methods, and classes.
2.1 Purpose of Final
The final keyword serves several important purposes:
- Prevents accidental modifications
- Improves code security
- Enables compiler optimizations
- Makes code more maintainable
Note
While final variables cannot be reassigned, their internal state can still be modified if they reference mutable objects.
3. Final Variables
Final variables are constants that cannot be modified after initialization:
3.1 Primitive Variables
// Final primitive variable
final int MAX_VALUE = 100;
// MAX_VALUE = 200; // Compilation error - cannot reassign
3.2 Reference Variables
// Final reference variable
final List names = new ArrayList<>();
names.add("John"); // Allowed - modifying object state
// names = new ArrayList<>(); // Compilation error - cannot reassign
3.3 Final Parameters
public void process(final String input) {
// input = "new value"; // Compilation error
System.out.println(input);
}
4. Final Methods
Final methods cannot be overridden by subclasses:
4.1 Method Declaration
public class Parent {
public final void display() {
System.out.println("Parent display");
}
}
public class Child extends Parent {
// @Override
// public void display() { // Compilation error
// System.out.println("Child display");
// }
}
4.2 Use Cases
- Prevent method overriding for security
- Ensure consistent behavior in inheritance
- Enable compiler optimizations
5. Final Classes
Final classes cannot be extended:
5.1 Class Declaration
public final class String {
// Class implementation
}
// public class CustomString extends String { // Compilation error
// // Cannot extend final class
// }
5.2 Common Final Classes
StringIntegerDoubleMath
Best Practice
Make a class final when you want to prevent inheritance and ensure that the class's behavior cannot be modified by subclasses.
6. Best Practices
When using the final keyword:
- Use final for constants and configuration values
- Make methods final when their behavior should not be changed
- Consider making utility classes final
- Use final parameters in methods to prevent accidental modifications
- Document why a class, method, or variable is final
Important
Remember that final variables can still be modified if they reference mutable objects. For true immutability, ensure the referenced objects are also immutable.
7. Conclusion
The final keyword is a powerful tool in Java that helps create more robust and maintainable code. It enforces immutability where needed and prevents unwanted modifications.
Key takeaways:
- Final variables cannot be reassigned
- Final methods cannot be overridden
- Final classes cannot be extended
- Use final judiciously to improve code quality