Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixed risk assessment number

...

Wiki Markup
The Java memory model guarantees that any final fields of an object are fully initialized before a published object becomes visible \[[Goetz 06|AA. Java References#Goetz 06]\]. By declaring {{n}} as final, the {{Helper}} class is made [immutable|BB. Definitions#immutable]. Furthermore, if the {{helper}} field is declared {{volatile}} in compliance with [CON01VNA01-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.

...

This compliant solution requires that helper be declared as volatile and class Helper be immutable. If it were not immutable, the code would violate CON06-J. Do not assume that declaring an object reference volatile guarantees visibility of its members and additional synchronization would be necessary (see the next compliant solution). And if the helper field were not volatile, it would violate CON01 VNA01-J. Ensure visibility of shared references to immutable objects.

Similarly, a public static factory method that returns a new instance of Helper can be provided in class Helper. This approach allows the Helper instance to be created in a private constructor.

...