Versions Compared

Key

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

...

This compliant solution requires that helper be declared as volatile and class Helper be immutable. If it were not immutable, the code would violate CON11CON06-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-J. Ensure visibility of shared references to immutable objects.

...

Because the Helper object can change state after its construction, synchronization is necessary to ensure visibility of mutable members after initial publication. Consequently, the setN() method is synchronized to provide visibility of n in this compliant solution (see CON11CON06-J. Do not assume that declaring an object reference volatile guarantees visibility of its members).

...

If the helper field in class Foo is not declared as volatile, the field n should be declared as volatile so that a happens-before relationship is established between the initialization of n and the write of Helper to the field helper. This is in compliance with CON11CON06-J. Do not assume that declaring an object reference volatile guarantees visibility of its members. This is only required when the caller (class Foo) cannot be trusted to declare helper as volatile.

...