ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
final field in Java is a class or instance variable that can be assigned a value exactly once. Once initialized, its value (for primitives) or memory reference (for objects) becomes strictly immutable and cannot be reassigned during the lifetime of the object or class.
Initialization Rules
The Java compiler enforces strict definite assignment rules forfinal fields. A final field must be initialized before the constructor (or static initialization, if static) completes.
A final field that is not initialized at the point of declaration is called a blank final field.
- Instance Final Fields: Must be initialized either at the point of declaration, within an instance initializer block, or within every constructor of the class.
- Static Final Fields: Must be initialized either at the point of declaration or within a
staticinitializer block.
Reference Immutability vs. Object State Immutability
Applying thefinal keyword to a reference type guarantees reference immutability, not object state immutability. The field cannot be reassigned to point to a different memory location, but the internal state of the referenced object can still be mutated if the object itself is mutable.
Java Memory Model (JMM) Semantics
Under the Java Memory Model (JSR-133),final fields provide a specific thread-safety guarantee known as initialization safety.
When an object is constructed, the thread performing the initialization writes to the final fields. The JMM guarantees that once the constructor completes, any other thread that acquires a reference to that object will immediately see the correctly initialized values of its final fields, without requiring explicit synchronization (like volatile or synchronized).
This guarantee is subject to the no-escape rule: the this reference of the object being constructed must not escape the constructor. If this is published to another thread before the constructor completes, that thread may observe the final fields in their default (uninitialized) states.
Reflection and Final Fields
Historically,final fields could be modified at runtime using the Java Reflection API via Field.setAccessible(true). However, modern Java enforces strict security boundaries that restrict this capability to prevent runtime instability:
- Since Java 12: Modifying
static finalfields via reflection is strictly prohibited and throws anIllegalAccessException. - Since Java 15: Modifying
finalfields insideRecordclasses is also blocked and throws anIllegalAccessException.
final field violates JMM guarantees. The JVM aggressively inlines final fields (especially constants), meaning reflective modifications may not be visible to code that has already cached the original value, leading to unpredictable behavior.
Master Java with Deep Grasping Methodology!Learn More





