The final Keyword in Java
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.
A final variable
Can be assigned exactly once; for a primitive, its value can never change again afterward.
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.
A final method
Cannot be overridden by any subclass, guaranteeing its exact behavior no matter how the class is extended.
A final class
Cannot be extended by any other class at all.
Why String is final
So its carefully-designed immutability guarantee can never be broken by a subclass overriding its behavior unexpectedly.
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); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.