Interview Q&A · Java · 4 questions in this topic

The final Keyword — Java Interview Q&A

All Java topics

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

  1. 1Final classes prevent extension but still allow use through composition.
  2. 2Final methods lock behavior while still allowing inheritance of other members.
  3. 3Final references cannot be reassigned, although the referenced object may still be mutable.
  4. 4A static final field of a primitive or String is a compile-time constant, so its value is inlined at every use site.
  5. 5Correctly published final fields are guaranteed visible after construction, which makes immutable classes easier to write.

Example

Example.javajava
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

  1. 1final applies to the reference, not the referent.
  2. 2Collections.unmodifiableList wraps but does not copy.
  3. 3Defensive copying protects encapsulated mutable state.
  4. 4Immutability needs the whole class to cooperate.

Example

Example.javajava
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

  1. 1Declared final, assigned later, exactly once.
  2. 2Instance blank finals are assigned in constructors.
  3. 3Static blank finals are assigned in a static block.
  4. 4The compiler checks definite assignment on every path.

Learn this topic

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits