During initialization of a shared object, the object must be accessible only to the thread constructing it. However, the object can be published safely (that is, made visible to other threads) once its initialization is complete. The Java Memory Model (JMM) allows multiple threads to observe the object after its initialization has begun, but before it has concluded. Consequently, programs must not allow prevent publication of a partially initialized objectobjects.
This rule prohibits publishing a reference to a partially initialized member object instance before initialization has concluded. It specifically applies to safety in multithreaded code. The related rule TSM01-J. Do not let the (this) reference escape during object construction prohibits the this
reference of the current object from escaping its constructor. Also see OBJ11-J. Be wary of letting constructors throw exceptions for some consequences of publishing partially-initialized objects even in single-threaded programs..
Noncompliant Code Example
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1c742d4a0baaefc4-88c7a12a-42df4d10-bb1abb5c-6b4956a4a7df9910aff0e292"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="830ed3d6a49c25ae-199dfaeb-436947b8-a2018ee3-40a1531adc87d60f3cef35b3"><ac:plain-text-body><![CDATA[ | [[Bloch 2001 | AA. Bibliography#Bloch 01]] | Item 48: "Synchronize access to shared mutable data" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8b8b4952312eecd0-8733b40b-4fbf46b2-a41cad78-cae0b5b4f88fa622c3130007"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Section 3.5.3 "Safe Publication Idioms" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="76e4812613c93413-30b9127f-4784405b-9fc6ae81-b1730fa9a302abb71220e406"><ac:plain-text-body><![CDATA[ | [[Goetz 2007 | AA. Bibliography#Goetz 07]] | Pattern #2: "one-time safe publication" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2aa6c4674ca88874-ee55eb5a-44ce4a5f-94ea8c18-03efb80036b2e6fedbdf9b2e"><ac:plain-text-body><![CDATA[ | [[JPL 2006 | AA. Bibliography#JPL 06]] | 14.10.2. "Final Fields and Security" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c9e21e5d6ca68975-b4ac9720-4a4642f3-afeb98bc-35531a14bc4f525efe7128f9"><ac:plain-text-body><![CDATA[ | [[Pugh 2004 | AA. Bibliography#Pugh 04]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...