Java final
field semantics
Contrary to popular belief, a final
field in Java is not the same thing as
a final
variable that may only be assigned to once.
The Java memory model (JMM) not only has a separate
section explaining the semantics
but also introduces two partial orders—dereference chain (dereferences
) and memory chain (mc
)
just for the sake of specifying final
fields.
This presentation touches almost all aspects of the JMM and has many examples of programs which behavior a reader may find unexpected. I presented it to my colleagues at NetCracker Technology and at ZeptoLab. Vladimir Sitnikov, who is a co-author of the presentation, made a public talk (in Russian)
final
field semantics
Links to useful resources about the JMM and memory models in general
- "Теоретический минимум для понимания Java Memory Model" (in Russian) by Roman Elizarov
- "Close Encounters of The Java Memory Model Kind" by Aleksey Shipilëv
- "Using JDK 9 Memory Order Modes" by Doug Lea
- "A Formalization of Java's Concurrent Access Modes" by John Bender, Jens Palsberg
- "The JSR-133 Cookbook for Compiler Writers" by Doug Lea
- "Hardware Memory Models", "Programming Language Memory Models" by Russ Cox
- "What do
Atomic*::lazySet
/Atomic*FieldUpdater::lazySet
/Unsafe::putOrdered*
actually mean?" by Nitsan Wakart