 
                            ...
Because the the Helper class is declared as public, it uses a private lock to handle synchronization in conformance with CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code.
Exceptions
Wiki Markup 
This exception uses a volatile initialized flag. The corresponding Foo class is the same as the noncompliant code example. 
| Code Block | ||
|---|---|---|
| 
 | ||
| 
public class Helper {
  private int n;
  private volatile boolean initialized; // Defaults to false
  public Helper(int n) {
    this.n = n;
    this.initialized = true;
  }
  
  public void doSomething() {
    if (!initialized) {
      throw new SecurityException("Cannot use partially initialized instance");
    }
    // ... 
  }
  // ...
}
 | 
This ensures that even if the reference to the Helper object instance is published before its initialization is over, the instance is unusable. The instance is unusable because every method within Helper must check the flag to determine whether the initialization has finished. 
 Classes that prevent partially initialized objects from being used may publish partially initialized objects. This may be implemented, for example, by setting a volatile boolean flag in the last statement of the initializing code and then ensuring this flag was set before allowing the execution of any class methods. 
The following compliant solution illustrates this technique:
| Code Block | ||
|---|---|---|
| 
 | ||
| 
public class Helper {
  private int n;
  private volatile boolean initialized; // Defaults to false
  public Helper(int n) {
    this.n = n;
    this.initialized = true;
  }
  
  public void doSomething() {
    if (!initialized) {
      throw new SecurityException("Cannot use partially initialized instance");
    }
    // ... 
  }
  // ...
}
 | 
This ensures that even if the reference to the Helper object instance is published before its initialization is over, the instance is unusable. The instance is unusable because every method within Helper must check the flag to determine whether the initialization has finished. Wiki Markup 
Risk Assessment
Failing to synchronize access to shared mutable data can cause different threads to observe different states of the object or a partially initialized object.
...