...
This safe publication guarantee applies only to primitive fields and object references. Programmers commonly use imprecise terminology and speak about "member objects." For the purposes of this visibility guarantee, the actual member is the object reference; the objects referred to (hereafter known as the referents) by volatile object references are beyond the scope of the safe publication guarantee. Consequently, declaring an object reference to be volatile is insufficient to guarantee that changes to the members of the referent are published to other threads. That is, a thread may fail to observe a recent write from another thread to a member field of such an object referent. Furthermore, when the referent is mutable and lacks thread-safety, other threads might see a partially constructed object or an object in a (temporarily) inconsistent state [Goetz 2007]. However, when the referent is immutable, declaring the reference volatile suffices to guarantee safe publication of the members of the referent. Consequently, programmers must not use the volatile
keyword to guarantee safe publication of mutable objects; use of the volatile
keyword to guarantee safe publication of primitive fields, object references, or fields of immutable object referents is permitted.
Confusing a volatile object with the volatility of its member objects is a similar error to the one described in OBJ50-JG. Never confuse the immutability of a reference with that of the referenced object.
Noncompliant Code Example (Arrays)
...