Introduction
In Object-Oriented Programming (OOP), the final
keyword in Java plays a critical role in shaping inheritance, immutability, and method behavior. Whether you're designing immutable data classes or securing core logic from being overridden, final
is a must-know tool in your design arsenal.
Let’s demystify the use of final
in Java, understand its implications on classes, methods, and variables, and see where it fits into large-scale Java systems.
What is the final
Keyword in Java?
In Java, final
is a non-access modifier used for:
- Preventing modification (immutability)
- Blocking method overrides
- Restricting inheritance
The meaning of final
changes slightly depending on whether it's used with a class, method, or variable.
Final Class
A final class cannot be extended (i.e., it has no subclasses).
Syntax
public final class Vehicle {
public void start() {
System.out.println("Starting the vehicle");
}
}
Trying to inherit from it:
public class Car extends Vehicle { // ❌ Compilation error
}
Use Case
- Making core logic non-extensible (e.g.,
String
,Integer
classes in JDK). - Security and predictability in APIs.
Final Method
A final method cannot be overridden by subclasses.
Syntax
public class Animal {
public final void breathe() {
System.out.println("Breathing...");
}
}
public class Dog extends Animal {
@Override
public void breathe() { // ❌ Compilation error
System.out.println("Dog breathing...");
}
}
Use Case
- Lock behavior for subclasses to preserve logic integrity.
- Prevent unexpected polymorphic overrides.
Final Variable
A final variable becomes a constant (i.e., cannot be reassigned once initialized).
Syntax
final int MAX_SPEED = 100;
MAX_SPEED = 120; // ❌ Compilation error
With Objects
final Car car = new Car();
car = new Car(); // ❌ Not allowed
car.setColor("Red"); // ✅ Allowed (object's state can change)
Real-World Example
final class Config {
public static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
public static final int TIMEOUT = 5000;
}
- The class can't be extended.
- The constants can’t be changed.
- Perfect for shared configuration.
UML Structure
+---------------------+
| FinalClass | <<final>>
+---------------------+
| - FINAL_CONSTANT |
+---------------------+
| +finalMethod() |
+---------------------+
Pros and Cons
Pros | Cons |
---|---|
Ensures immutability | Can limit extensibility |
Improves security | Less flexible in extensible frameworks |
Boosts clarity and predictability | Might lead to duplicate code in hierarchy |
Best Practices
- Use
final
for constants and stateless utility classes. - Make method
final
only if overriding would introduce bugs. - Avoid overusing
final
in large inheritance chains—consider design alternatives.
Refactoring Tip
If a method should not be overridden due to business rules, refactor it to be final
and document the reason.
Java 17/21 Notes
- Java 17 introduces
sealed classes
as an alternative tofinal
that offers more controlled inheritance. record
types are implicitlyfinal
and immutable—no need for manualfinal
usage.
Real-World Analogy
Think of final
like a “sealed envelope.” Once sealed (assigned or implemented), no one can reopen and rewrite it.
Conclusion
The final
keyword in Java offers a powerful tool for designing robust, secure, and predictable systems. Whether you're locking down class hierarchies, protecting method behavior, or making variables immutable, mastering final
is essential for every Java developer.
Key Takeaways
final class
: Cannot be extended.final method
: Cannot be overridden.final variable
: Cannot be reassigned.- Use
final
to enforce rules and clarity in your Java OOP design.
FAQ – Final Keyword in Java
1. Can I override a final method in Java?
No. Final methods cannot be overridden in any subclass.
2. Can a final class have non-final methods?
Yes. You can define regular methods in a final class—they just can’t be inherited.
3. Can a final variable be initialized later?
Yes, if it's a blank final—it must be initialized in the constructor.
4. Is the final keyword the same as constant in Java?
Almost. final
ensures single assignment; constants are usually static final
.
5. Can I inherit from a class that has only final methods?
Yes, as long as the class itself is not declared final
.
6. What is the impact on performance?
The JVM may optimize final methods due to certainty of implementation.
7. Can final variables be used in anonymous classes?
Yes. Since Java 8, effectively final variables can also be used.
8. How is final different from sealed classes?
Final blocks all inheritance. Sealed allows selective subclassing.
9. Should I make every variable final?
Use it where immutability makes sense—especially for shared or thread-safe data.
10. Can I use final on constructors?
No. Constructors are inherently final and cannot be overridden.