Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
However, this solution requires the assignment of a new {{Helper}} instance to {{helper}} from Foo's constructor. According to the _Java Language Specification_, Section 17.5.2, "Reading Final Fields During Construction" \[[JLS 2005|AA. JavaBibliography#JLS References#JLS 05]\]

A read of a final field of an object within the thread that constructs that object is ordered with respect to the initialization of that field within the constructor by the usual happens-before rules. If the read occurs after the field is set in the constructor, it sees the value the final field is assigned, otherwise it sees the default value.

...

Wiki Markup
According to JSR-133, Section 9.2.3, "Static Final Fields"  \[[JSR-133 2004|AA. Java References#JSRBibliography#JSR-133 04]\]:

The rules for class initialization ensure that any thread that reads a static field will be synchronized with the static initialization of that class, which is the only place where static final fields can be set. Thus, no special rules in the JMM are needed for static final fields.

...

Wiki Markup
The JMM guarantees that any final fields of an object are fully initialized before a published object becomes visible \[[Goetz 2006|AA. JavaBibliography#Goetz References#Goetz 06]\]. By declaring {{n}} final, the {{Helper}} class is made [immutable|BB. Definitions#immutable]. Furthermore, if the {{helper}} field is declared volatile in compliance with guideline [VNA01-J. Ensure visibility of shared references to immutable objects], {{Helper}}'s reference is guaranteed to be made visible to any thread that calls {{getHelper()}} after {{Helper}} has been fully initialized.

...

Wiki Markup
\[[API 2006|AA. JavaBibliography#API References#API 06]\]
\[[Bloch 2001|AA. Java References#BlochBibliography#Bloch 01]\] Item 48: "Synchronize access to shared mutable data"
\[[Goetz 2006|AA. JavaBibliography#Goetz References#Goetz 06]\] Section 3.5.3 "Safe Publication Idioms"
\[[Goetz 2007|AA. Java References#GoetzBibliography#Goetz 07]\] Pattern #2: "one-time safe publication"
\[[JPL 2006|AA. JavaBibliography#JPL References#JPL 06]\] 14.10.2. "Final Fields and Security"
\[[Pugh 2004|AA. Java References#PughBibliography#Pugh 04]\]

...

LCK10-J. Do not use incorrect forms of the double-checked locking idiom      12. Locking (LCK)