Java Reference ยท Keywords and Statements
The final keyword
Reference for the Java final keyword: constants, final fields and parameters, final methods that cannot be overridden, and final classes that cannot be extended.
final means "cannot be changed after this point". Its exact meaning depends on what it modifies. A final variable can be assigned only once. A final method cannot be overridden. A final class cannot be extended.
final never makes an object immutable: it fixes a variable's reference, not the state of the object it points to. A final List<String> can still have elements added.
final int max = 10; // variable static final double TAX = 0.2; // constant final class Money { } // cannot be subclassed class Base { final void id() { } } // cannot be overridden
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
| Applied to | Rule |
|---|---|
| Local variable | Must be definitely assigned exactly once before use. |
| Parameter | Cannot be reassigned inside the method. |
| Instance field | Must be assigned once in an initializer or every constructor. |
static final field |
Must be assigned once in its declaration or a static initializer. Constant names conventionally use UPPER_SNAKE_CASE. |
| Method | Subclasses cannot override it. private and static methods cannot be overridden anyway. |
| Class | Cannot appear in an extends clause. Also cannot be abstract or sealed. |
Usage notes
- Compile-time constants: a
finalvariable of a primitive orStringtype initialized with a constant expression is inlined by the compiler and can be used incaselabels. - Reference vs content:
finalon a reference stops reassignment only. For real immutability, make the object's fields final and avoid exposing mutable parts, or useList.of()and records. - Effectively final: a local variable that is never reassigned is "effectively final" even without the keyword. Lambdas and inner classes can capture only final or effectively final locals.
- Memory safety: after a constructor finishes, other threads see the correct values of final fields even without synchronization, provided
thisdid not escape during construction. - Design: making a class final blocks extension, which protects invariants (
String,Integerand all records are final). Prefer final classes unless you design for inheritance. - Illegal combinations:
final abstract(for classes and methods) is rejected because abstract members need a subclass.finalwithvolatileon a field is also rejected. - Enhanced for:
for (final String s : list)is legal and documents that the loop variable is not reassigned. - Not reflection-proof: a final field is not guaranteed to stay unchanged when reflection is involved; do not rely on it for security.
Related
static,abstract,sealed,record,var- Lessons: The final keyword everywhere, Constants, Immutable classes