Learn Java
import com.learnjava.advancedjava.FinalKeyword;

The final Keyword in Java

@Concept

final means something can be set once and never changed again, and it applies differently depending on what it's attached to. A final variable can be assigned exactly once — for a primitive, that means its value can never change afterward; for a reference type, it means the variable can never be reassigned to point at a different object, though the object it already points to can still have its own fields modified freely, since final only locks the reference itself, not what it points to.

A final method cannot be overridden by any subclass, guaranteeing its behavior stays exactly as written no matter how the class is extended. A final class cannot be extended at all — String itself is a well-known final class, specifically so its carefully-designed immutability guarantee can never be broken by some subclass that overrides its behavior in an unexpected way.

@HowItWorks
1

A final variable

Can be assigned exactly once; for a primitive, its value can never change again afterward.

2

final on a reference variable

The variable itself can never be reassigned to a different object, though that object's own fields can still change freely.

3

A final method

Cannot be overridden by any subclass, guaranteeing its exact behavior no matter how the class is extended.

4

A final class

Cannot be extended by any other class at all.

5

Why String is final

So its carefully-designed immutability guarantee can never be broken by a subclass overriding its behavior unexpectedly.

@Example
public class FinalDemo {
static final int MAX_SIZE = 100;
 
public static void main(String[] args) {
final StringBuilder sb = new StringBuilder("Hello");
sb.append(", world!");
System.out.println(sb);
System.out.println("Max size: " + MAX_SIZE);
}
}
Output
Hello, world!
Max size: 100
1sb is declared final, so it can never be reassigned to point at a different StringBuilder object.
2sb.append(", world!") still modifies that same object's own internal contents freely — final never locked the object itself, only the variable's reference to it.
3MAX_SIZE, a final static field, was assigned once and could never be changed anywhere else in the program.
4Both println calls run once each, showing the mutated StringBuilder's contents and the unchanging constant.
@UseCases
Declaring a genuine constant, like a maximum size or a fixed configuration value, with final
Guaranteeing a reference variable, like one captured in a lambda, is never reassigned after being set
Sealing a class's specific behavior against being overridden by any future subclass
Understanding why String is declared final and can never be extended by a custom subclass

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.