Versions Compared

Key

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

...

Variables declared static are guaranteed to be initialized and made visible to other threads immediately. Static initializers also exhibit these properties. This approach should not be confused with eager initialization because in this case, the Java Language Specification guarantees lazy initialization of the class when it will be first used.

Compliant Solution (explicit static lazy initialization)

This compliant solution incorporates lazy initialization which makes it more productive. It also uses a static variable as suggested in the previous compliant solution. The variable is declared within a static inner, Holder class.

Code Block
bgColor#ccccff
class Foo {
  static final Helper helper;

  // Lazy initialization 
  private static class Holder {
    static Helper helper = new Helper();
  }

  public static Helper getInstance() {
    return Holder.helper;
  }
}

...