final
The final keyword fixes a binding after it is set once. It stops a variable being reassigned, a method being overridden, or a class being extended, depending on where you put it.
Detailed Explanation
One keyword, three unrelated jobs. That is the first thing to get straight, because learners often assume the three meanings are the same idea. They are not.
On a variable or a field, final means "assign exactly once". After that, the compiler rejects any attempt to point the name at something else. Say you are caching user sessions. A field private final List<String> tokens = new ArrayList<>(); guarantees that every method in the class talks to the same list object for the life of the instance. Nobody can swap the list out from underneath you halfway through a request.
Here is the exception, and it is the one that matters most: final locks the reference, not the object. The list is still mutable. You can add and remove tokens all day. If you want the contents frozen too, you need an immutable type such as List.of(...), a record, or a defensive copy. Final and immutable are different words for a reason.
A field with no initialiser at the declaration is called a blank final. Every constructor must assign it exactly once, on every code path, or the class does not compile. This is how records work internally: every record component is a final field assigned by the canonical constructor.
Final fields also carry a real concurrency guarantee, which is easy to miss. Under the Java memory model, a thread that sees a properly constructed object is guaranteed to see the correctly initialised values of its final fields, without any synchronisation. Non-final fields have no such promise; another thread can observe them as still holding default values. This is why immutable value objects built from final fields are safe to publish across threads and mutable ones are not.
On a method, final means no subclass may override it. On a class, final means no subclass at all. String is final for exactly this reason: if you could subclass it, you could hand a mutable "string" to code that assumed strings never change, and every security check based on a filename or a URL would be defeatable. Java 17 added sealed classes as the middle option, where you name the permitted subclasses instead of banning all of them.
Since Java 8 you also meet the term "effectively final". A local variable that is never reassigned counts as final even without the keyword, and only such variables can be captured by a lambda or an anonymous class. The compiler copies the value into the lambda, so a variable that could change afterwards would be ambiguous.
What does it cost? At runtime, almost nothing. The JIT compiler had already been proving most of these facts by itself, so final on locals is a readability tool rather than a speed tool. The real cost is design: a final class cannot be extended by test doubles or by future callers, so choose it deliberately rather than by habit.
Code Examples
import java.util.ArrayList;
import java.util.List;
// final class: nobody can subclass this and change how sessions are stored.
public final class SessionCache {
// Blank final: no initialiser here, so every constructor must assign it.
private final int maxEntries;
// The reference is fixed for the life of the object. The list is not frozen.
private final List<String> tokens = new ArrayList<>();
public SessionCache(int maxEntries) {
this.maxEntries = maxEntries; // exactly one assignment, on every path
}
public void add(String token) {
// Allowed: we mutate the object the field points at.
if (tokens.size() < maxEntries) {
tokens.add(token);
}
// tokens = new ArrayList<>(); // rejected: cannot reassign a final field
}
public int size() {
return tokens.size();
}
}import java.util.List;
import java.util.function.Predicate;
class Filters {
static Predicate<String> longerThan(int limit) {
// 'limit' is never reassigned, so it is "effectively final".
// Only effectively final locals may be captured by a lambda.
return token -> token.length() > limit;
}
public static void main(String[] args) {
var tokens = List.of("ab", "abcdef");
System.out.println(tokens.stream().filter(longerThan(3)).count()); // prints: 1
}
}Common Gotchas
final is not immutable
a `final Map<String, User> cache` can still be cleared, filled, or corrupted by any caller you hand it to. For genuine immutability use `Map.of`, `List.copyOf`, or a record with immutable components, and never leak the internal collection from a getter.
A blank final must be assigned on every path
if one constructor branch forgets it, or if you assign it twice, compilation fails. Assign it in the constructor body or in an instance initialiser, not conditionally.
Effectively final is why loop counters break lambdas
a classic `for (int i = 0; ...)` counter is reassigned each iteration, so a lambda cannot capture it. The enhanced for loop variable is fresh per iteration, so that one works.
final on a static field plus a compile-time constant means inlining
`static final int MAX = 100;` is copied straight into the bytecode of every class that reads it. Change the value and recompile only that class, and stale callers keep the old number until they are recompiled too.