...
Code Block | ||
---|---|---|
| ||
class Foo { private final Helper helper; public Helper getHelper() { return helper; } public Foo() { // Point 1 helper = new Helper(42); // Point 2 } } |
However, this solution requires the assignment of a new {{ Wiki Markup Helper
}} instance to {{helper
}} from Foo's constructor. According to the _Java Language Specification_, [§17, §17.5.2, "Reading Final Fields During Construction"|http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5.2] \[[JLS 2005|AA. References#JLS 05]\]Construction" [JLS 2005]
A read of a final field of an object within the thread that constructs that object is ordered with respect to the initialization of that field within the constructor by the usual happens-before rules. If the read occurs after the field is set in the constructor, it sees the value the final field is assigned; otherwise, it sees the default value.
...
The helper
field should be declared final to document the class's immutability.unmigrated-wiki-markup
According to JSR-133, Section 9.2.3, "Static Final Fields" \[ [JSR-133 2004|AA. References#JSR-133 04]\]
The rules for class initialization ensure that any thread that reads a
static
field will be synchronized with the static initialization of that class, which is the only place where static final fields can be set. Thus, no special rules in the JMM are needed for static final fields.
Compliant Solution (Immutable Object - Final Fields, Volatile Reference)
...
The JMM guarantees that any final fields of an object are fully initialized before a published object becomes visible \[ [Goetz 2006a|AA. References#Goetz 06]\]. By declaring {{n}} declaring n
final, the {{Helper
}} class is made [immutable|BB. Glossary#immutable]. Furthermore, if the {{helper
}} field is declared volatile in compliance with rule [VNA01-J. Ensure visibility of shared references to immutable objects], {{Helper
}}'s reference is guaranteed to be made visible to any thread that calls {{getHelper()
}} only after {{Helper
}} has been fully initialized.
Code Block | ||
---|---|---|
| ||
class Foo { private volatile Helper helper; public Helper getHelper() { return helper; } public void initialize() { helper = new Helper(42); } } // Immutable Helper public final class Helper { private final int n; public Helper(int n) { this.n = n; } // ... } |
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
TSM03-J | medium | probable | medium | P8 | L2 |
Bibliography
[API 2006] |
| |||
Item 48. Synchronize access to shared mutable data | ||||
Section 3.5.3, Safe Publication Idioms | ||||
Pattern #2, One-Time Safe Publication | ||||
[JPL 2006] | 14.10.2, Final Fields and Security | |||
| ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fff205a5-bab9-454d-9e9a-9764bb9c70ff"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. References#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="73812b7e-77d0-43ba-8f7b-4bb1899ec027"><ac:plain-text-body><![CDATA[ | [[Bloch 2001 | AA. References#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="04ec2d58-d716-411a-b6c6-e248d117a20e"><ac:plain-text-body><![CDATA[ | [[Goetz 2006a | AA. References#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="ca895e65-a539-4aa0-9f77-4dce46fe2b1d"><ac:plain-text-body><![CDATA[ | [[Goetz 2007 | AA. References#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="d9038986-6e0a-4dd7-bce0-11746538dbe4"><ac:plain-text-body><![CDATA[ | [[JPL 2006 | AA. References#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="e8806c9e-3830-462e-97b7-664d0a8a8977"><ac:plain-text-body><![CDATA[ | [[Pugh 2004 | AA. References#Pugh 04]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...
11. Thread-Safety Miscellaneous (TSM) 12. Input Output (FIO)