What is the final Keyword in Java?
Learn what the final keyword does in Java for variables, methods, and classes, and how it differs from true immutability with clear examples.
Expected Interview Answer
The final keyword marks something as unmodifiable once set: a final variable's reference cannot be reassigned, a final method cannot be overridden by a subclass, and a final class cannot be extended.
For a final variable holding a primitive, the value itself is locked after initialization; for a final variable holding an object reference, the reference cannot point elsewhere but the object's own internal state can still change (unless the object is itself immutable). Final methods let a class guarantee that certain behavior can never be overridden, which protects invariants relied on elsewhere in the class. Final classes like String and Integer prevent subclassing entirely, which is often done for immutability and security. final is also required for local variables captured by anonymous classes or lambdas (or must be effectively final).
- Prevents accidental reassignment of important variables
- Protects critical methods from being overridden incorrectly
- Enables safe subclass-proof design for security-sensitive classes
- Supports building truly immutable objects
- Required for variables captured inside lambdas/anonymous classes
AI Mentor Explanation
A final variable is like a toss result announced at the start of the match — once called, it cannot be redone for that game, though the match itself can still unfold in many ways. A final method is like an umpire's LBW decision-making rule that no team is allowed to reinterpret, and a final class is like a national team that no franchise league is permitted to rebrand under its own name.
Step-by-Step Explanation
Step 1
Final variables
Once assigned, a final variable's reference (or primitive value) cannot be reassigned again.
Step 2
Final and mutability
A final reference variable still allows the referenced object's internal state to change unless that object is itself immutable.
Step 3
Final methods
Marking a method final prevents any subclass from overriding it, locking in its behavior.
Step 4
Final classes
Marking a class final prevents any other class from extending it at all, as with String and Integer.
Step 5
Final in lambdas
Local variables captured by a lambda or anonymous class must be final or effectively final so the captured value is stable.
What Interviewer Expects
- Distinguishes final variable, final method, and final class use cases
- Knows a final object reference still allows internal mutation of that object
- Can name real final classes like String and explain why they're final
- Understands effectively final in lambda capture
- Knows final does not by itself make an object immutable
Common Mistakes
- Believing final makes an object's entire state immutable
- Confusing final with static (they solve different problems)
- Thinking a final method can still be overridden if annotated with @Override
- Forgetting local variables used in lambdas must be final or effectively final
- Assuming final classes can still be subclassed within the same package
Best Answer (HR Friendly)
“The final keyword in Java means something cannot be changed after it's set. A final variable can't be reassigned, a final method can't be changed by a subclass, and a final class can't be extended by anyone — it's a way of locking down parts of your code to prevent accidental or unsafe changes.”
Code Example
final class ImmutablePoint { // cannot be subclassed
final int x; // cannot be reassigned after constructor
final int y;
ImmutablePoint(int x, int y) {
this.x = x;
this.y = y;
}
final String describe() { // cannot be overridden
return "(" + x + ", " + y + ")";
}
}
public class Main {
public static void main(String[] args) {
ImmutablePoint p = new ImmutablePoint(3, 4);
System.out.println(p.describe()); // (3, 4)
// p.x = 5; // compile error: cannot assign a value to final variable x
}
}Follow-up Questions
- Does final make an object immutable? Why or why not?
- What does 'effectively final' mean in the context of lambdas?
- Why are String and Integer declared as final classes?
- Can a final method still be inherited and called by a subclass?
- What is the difference between final and static final fields?
MCQ Practice
1. What does a final method prevent?
A final method can still be called and inherited normally, it just cannot be overridden by any subclass.
2. If a final variable holds an object reference, what can still change?
final locks the reference, not the referenced object's internal fields, unless that object is separately immutable.
3. Why is the String class declared final in Java?
String is final so that its immutability contract, used throughout the JVM (e.g. string pool, hashing), cannot be broken by subclassing.
Flash Cards
What does a final variable prevent? — Reassignment of that variable's reference or value after initialization.
What does a final method prevent? — Subclasses from overriding that method's implementation.
What does a final class prevent? — Any other class from extending it.
Does final alone make an object immutable? — No — it only locks the reference; the object's own fields can still change unless designed to be immutable.