All topics
library
beginner

The final Keyword

Know every use of final: variables, methods, classes, and how it affects immutability and performance.

final prevents a change, and which change depends on where you put it.

Final variable = a picture frame bolted to the wall (can't move the frame, but can change the photo inside). Final class = a sealed envelope (can't open and modify it).

Key Concepts

1
On a variable, the reference cannot be reassigned.
    final List<String> names = new ArrayList<>();
    names.add("Asha");                  // fine -- the LIST is not final
    names = new ArrayList<>();          // will not compile
2
That is the trap. final on an object field freezes the reference, not the object. A final List can still be emptied and refilled by anyone holding it.
finalfinal List
3
For a primitive it really is constant, because there is nothing else to change:
4
    final int MAX = 100;
5
A blank final is declared without a value and must be assigned exactly once in every constructor:
6
    class Account {
      private final String id;
      Account(String id) { this.id = id; }     // required
    }
7
On a method, no subclass can override it. Use it when the behaviour is part of a guarantee the class makes and a subclass changing it would break that guarantee.
8
On a class, nothing can extend it. String, Integer and the other wrappers are final, because their immutability would otherwise be only a suggestion.
StringInteger
9
Effectively final is the Java 8 relaxation. A local variable never reassigned counts as final without the keyword, which is what lets a lambda capture it:
10
    int count = list.size();                   // never reassigned
    run(() -> print(count));                   // captures it
11
Reassign count anywhere and the lambda stops compiling.
count
12
Final fields also give you a threading guarantee. Once a constructor finishes, every thread sees the correct values of that object's final fields without any synchronisation. That is the basis of safe publication, and the reason immutable objects are safe to share.
13
final alone does not make an object immutable. You need every field final and no mutable internal state handed out.
final
14
What the interviewer is probing.1. "Does final make an object immutable?" Probing: the commonest misconception about final. Stalls: "Yes, it cannot change." Moves up: it only prevents reassigning the reference; a final List can still be added to. Immutability needs final fields and no leaked mutable state.
15
2. "What is 'effectively final' and why does it matter?" Probing: lambda capture. Stalls: "Same as final." Moves up: a local never reassigned counts as final without the keyword, which is what lets a lambda or anonymous class capture it.
16
3. "Why are String and the wrapper classes final?" Probing: why you would forbid extension. Stalls: "For performance." Moves up: their immutability is a guarantee the platform relies on — caching, the string pool and security checks — and a subclass could break it.
17
4. "What does final give you in multithreaded code?" Probing: safe publication, which most candidates miss. Stalls: "Nothing special." Moves up: once the constructor completes, every thread sees the correct values of the final fields without synchronisation — the basis for sharing immutable objects.