Java / Java 21 Coding Standards Interview Questions
What is the purpose of the final keyword in Java 21 coding standards?
final marks a variable, field, parameter, method, or class as unable to be reassigned or, for methods and classes, unable to be overridden or extended. On a local variable or field it means the reference is set once and never changed again.
public final class Money { private final BigDecimal amount; private final Currency currency; }
Standards recommend marking fields final by default and only removing it when mutation is genuinely required, because an immutable field can be reasoned about locally - its value at construction is its value forever - which removes an entire category of concurrency and aliasing bugs.
On a class, final communicates that the type is not designed to be extended, which is also why records are implicitly final: their behavior is defined entirely by their components, and allowing subclassing would undermine that guarantee.
More Related questions...