Interview Q&A · Java · 4 questions in this topic
The final Keyword — Java Interview Q&A
What final guarantees on classes, methods, variables, and references. Part of OOP Mechanics · 294 questions in the Java bank.
final is a compile-time guarantee about assignment, not about immutability. On a field it means the reference is written once; the object it points at can still change unless that object is itself immutable. On a class it prevents subclassing, which is how String and record types stay trustworthy; on a method it prevents an override that could silently change behaviour. Blank finals are legal when every constructor assigns them, and effectively final locals can be captured by lambdas — a rule that often surprises readers.
What does the final modifier do on classes, methods and variables?
Answer
A final class cannot be extended, with String, Integer, and records as familiar examples, which also makes such types secure against subclass tampering. A final method cannot be overridden, so a subclass inherits it exactly as written. A final variable can be assigned only once and acts as a constant, where blank finals must be set in every constructor and effectively final locals can be captured by lambdas and inner classes.
Key points
- 1Final classes prevent extension but still allow use through composition.
- 2Final methods lock behavior while still allowing inheritance of other members.
- 3Final references cannot be reassigned, although the referenced object may still be mutable.
- 4A static final field of a primitive or String is a compile-time constant, so its value is inlined at every use site.
- 5Correctly published final fields are guaranteed visible after construction, which makes immutable classes easier to write.
Example
public final class Config {} public class Base { public final void start() {} } public static final int MAX = 100;
💡 Remember that final on a reference locks the reference, not the object contents.
Does declaring a field final make the object immutable?
Answer
No. final freezes the reference, not the object it points at: a final List can still be added to, and a final array’s elements can still be reassigned. Immutability requires an immutable class — private final fields, no setters, and defensive copies for anything mutable that crosses the boundary. That difference is why code reviews ask for unmodifiable copies rather than final fields alone.
Key points
- 1final applies to the reference, not the referent.
- 2Collections.unmodifiableList wraps but does not copy.
- 3Defensive copying protects encapsulated mutable state.
- 4Immutability needs the whole class to cooperate.
Example
final List<String> names = new ArrayList<>(); names.add("Ada"); // allowed — the list is mutable // names = new ArrayList<>(); // compile error — the reference is final
What is a blank final variable?
Answer
A final variable declared without an initialiser. It must be definitely assigned before first use — exactly once on every path — which for an instance field means inside every constructor, and for a static field means inside the static initialiser. This is how immutable objects are built when the value depends on constructor arguments, and the compiler rejects any path that leaves the field unassigned.
Key points
- 1Declared final, assigned later, exactly once.
- 2Instance blank finals are assigned in constructors.
- 3Static blank finals are assigned in a static block.
- 4The compiler checks definite assignment on every path.
Can a final method be overridden, or a final class extended?
Answer
Neither. A final method cannot be overridden by a subclass, and a final class cannot be extended at all — String, Integer and other value types are final for exactly this reason. The purpose is to protect invariants and behaviour that the class depends on, such as the immutability contract of String. The cost is some flexibility, so use final on methods when a subclass override would genuinely break correctness.
Key points
- 1final methods cannot be overridden; final classes cannot be extended.
- 2String is final to preserve its immutability invariants.
- 3Sealing a class allows a fixed set of subclasses instead.
- 4final on a class is a stronger promise than final on a method.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission