OOP & Type Systemhigh

final Keyword Semantics

final on a variable: can be assigned only once. final on a method: cannot be overridden. final on a class: cannot be subclassed. String, Integer, and other core types are final classes.

Memory anchor

final = superglue. On a variable: the label is glued to one jar (can't point elsewhere, but jar contents can change). On a method: glued shut, no override. On a class: glued the family tree -- no children allowed.

Expected depth

final fields get special JMM treatment: after a constructor completes, a final field's value is guaranteed visible to all threads without synchronization (final field freeze). This is what makes immutable objects safely publishable. final local variables are required for use in lambdas and anonymous inner classes (effectively final in Java 8+). Declaring a method final lets the JIT inline it aggressively because there can be no polymorphic dispatch.

Deep — senior internals

The JMM final field freeze guarantee (JLS §17.5) specifies a 'freeze' action at the end of the constructor — all writes to final fields happen-before any read of those fields through any reference. This is stronger than the normal happens-before — it applies even if the reference escapes via a data race (unsafe publication), though fully safe publication still requires synchronization. Marking frequently called methods final can improve JIT performance because the JIT knows monomorphic dispatch is safe without needing to speculate and later deoptimize.

🎤Interview-ready answer

final has three distinct uses: a final variable can be assigned exactly once (enabling effectively-final in lambdas); a final method cannot be overridden, enabling JIT inlining without speculative deoptimization; a final class cannot be subclassed, which (combined with private final fields) is the foundation of immutability. The JMM's final field freeze guarantee is critical for safe concurrent publication of immutable objects — no synchronization required if all shared fields are final.

Common trap

final on a reference type variable means the reference cannot be reassigned, not that the referenced object is immutable — final List<String> list still allows list.add("x").