...
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 [CON09CON02-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 CON11-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 CON09CON02-J. Ensure visibility of shared references to immutable objects.
...