The final Keyword
The final keyword in Java locks something down: a final variable can be assigned only once, a final method cannot be overridden by a subclass, and a final class cannot be extended at all. C++11 introduced its own final, and C# uses sealed for classes, with the compiler enforcing each of these restrictions.
final is Java's keyword (C# uses sealed for classes and readonly/no-override rules for the rest; C++11 introduced its own final) for locking something down: a variable that can never be reassigned after its first assignment, a method that can never be overridden by a subclass, or a class that can never be extended (subclassed) at all.
How it works, by context
Final variables: can be assigned exactly once, either at declaration or in the constructor, after that, reassignment is a compile error.
Final methods: can be inherited and called normally, but a subclass is not allowed to override them, the implementation is locked.
Final classes: cannot be extended by any other class at all, there can be no subclasses.
A worked example
final class ImmutablePoint { // final class: nobody can extend this
final int x; // final field: set once, never reassigned
final int y;
ImmutablePoint(int x, int y) {
this.x = x;
this.y = y;
}
final double distanceFromOrigin() { // final method: can't be overridden (moot here since the class itself is final, but shown for clarity)
return Math.sqrt(x * x + y * y);
}
}
// class ExtendedPoint extends ImmutablePoint { } // compile error: cannot inherit from final class
public class Main {
public static void main(String[] args) {
ImmutablePoint p = new ImmutablePoint(3, 4);
System.out.println(p.distanceFromOrigin()); // prints 5.0
// p.x = 10; // compile error, x is final and already assigned
}
}
Because x and y are final, once the constructor sets them, nothing anywhere in the program can ever change them again, ImmutablePoint objects are guaranteed immutable by the compiler itself, not just by convention.
Where this matters in real systems
Java's own String class is final, precisely so that no one can create a subclass that changes string behavior in a way that would break the massive number of assumptions the rest of the language and standard library make about how strings work. Immutable data classes built with final fields are a core pattern in concurrent programming: an object that genuinely cannot change after construction can be freely shared between threads with zero risk of one thread seeing a half-updated, inconsistent version of it.
What interviewers actually ask about the final keyword
- "What are the three things
finalcan apply to in Java, and what does each do?" Variables (can't be reassigned after first assignment), methods (can't be overridden by subclasses), and classes (can't be subclassed at all). - "Why would you mark a class as
final?" To guarantee its behavior can never be silently changed by a subclass overriding methods in unexpected ways,Stringand Java's wrapper classes likeIntegerare real examples. - "Does
finalmean the object itself is immutable?" Not automatically, afinalreference to a mutable object (like afinal List) still lets you modify the contents of that list, it just prevents the variable from pointing to a different list entirely. - "How does
finalhelp with thread safety?" Afinalfield is guaranteed to be fully initialized before any thread can see the object, avoiding a class of subtle concurrency bugs where one thread observes a partially-constructed object.
Key takeaways
finalon a variable means it can be assigned exactly once and never reassigned.finalon a method means it can never be overridden by a subclass.finalon a class means it can never be extended at all.finaldoesn't automatically make an entire object deeply immutable, it only locks the specific thing it's applied to.
More in Keywords
Browse all OOP topics
Log in to save which topics you've read across every subject.
Frequently asked questions
What are the three things `final` can apply to in Java, and what does each do?
Variables (can't be reassigned after first assignment), methods (can't be overridden by subclasses), and classes (can't be subclassed at all).
Why would you mark a class as `final`?
To guarantee its behavior can never be silently changed by a subclass overriding methods in unexpected ways, `String` and Java's wrapper classes like `Integer` are real examples.
Does `final` mean the object itself is immutable?
Not automatically, a `final` reference to a mutable object (like a `final List`) still lets you modify the contents of that list, it just prevents the variable from pointing to a different list entirely.
How does `final` help with thread safety?
A `final` field is guaranteed to be fully initialized before any thread can see the object, avoiding a class of subtle concurrency bugs where one thread observes a partially-constructed object.