The final keyword in Java marks something as unchangeable once it is set. A final variable can be assigned only once, a final method cannot be overridden, and a final class cannot be extended. It makes intent explicit, helps the compiler catch mistakes, and is the foundation of immutable, thread-safe design.
One detail trips up many developers: final makes a reference constant, not the object it points to. This article covers each use, the immutability caveat and how final relates to modern Java features such as records and lambdas.
A final local variable, parameter or field can be assigned exactly once.
Rules to remember:
static final in upper snake case.Declaring a reference final stops you from pointing it at another object. It does not stop the object from changing.
For real immutability make the fields final, avoid setters, copy mutable inputs defensively and expose unmodifiable views, for example List.copyOf(items).
A final method cannot be overridden by subclasses. Use it when the behaviour is part of a contract that must not change, such as a template method that calls overridable steps.
A private method is already not overridable, so final adds nothing there. Calling an overridable method from a constructor is a bug source, and declaring such methods final avoids it.
A final class cannot be subclassed. String, Integer and the other wrapper classes are final, which is why they are safe to share and use as map keys.
Since Java 17, sealed classes offer a middle path: sealed restricts which classes may extend a type, while final forbids all extension. Subclasses of a sealed type must themselves be final, sealed or non-sealed.
Records are implicitly final, and their components are final fields, so a record is a concise way to declare a shallowly immutable data class.
Lambdas and anonymous classes can only capture local variables that are final or effectively final, meaning never reassigned after initialization. You do not need to write final in that case.
If you need mutable state in a lambda, use an AtomicInteger, a one-element array or restructure with streams.
Under the Java Memory Model, a properly constructed object's final fields are visible to other threads without extra synchronization, provided this does not escape during construction. This is why immutable classes with only final fields can be shared between threads safely, and it is a key reason to prefer them.
final on a method or class is not a reliable optimization. The JIT compiler already inlines non-overridden methods using class hierarchy analysis.final on local variables is optional style. Many teams use it only for fields and constants.static final field of a primitive or String initialized with a constant expression is a compile-time constant and is inlined into code that uses it, so changing it requires recompiling dependents.final is a modifier, finally is a block that runs after try and catch, and finalize() is a deprecated method of Object called by the garbage collector.
The variable cannot be reassigned, but if it refers to a mutable object, that object's state can change.
No. An abstract class needs subclasses, and a final class forbids them, so the compiler rejects the combination.
It is not required. It only prevents reassigning the parameter inside the method, and some teams enforce it through style rules.
Use final for constants, fields that never change after construction, methods whose behaviour must not be overridden and classes that must not be extended. Remember it freezes the reference, not the object, combine it with records and defensive copies for true immutability, and rely on effectively final variables for lambdas.