Versions Compared

Key

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

...

A class or an instance method is used for lazy initialization, depending on whether the member object is static. The method checks whether the instance has already been created and, if not, creates it. If the instance already exists, it simply returns it:

...

This compliant solution uses the initialize-on-demand, holder class idiom that implicitly incorporates lazy initialization by declaring a static variable within a static Holder inner class Holder.

Code Block
bgColor#ccccff
final class Foo {
  // Lazy initialization
  private static class Holder {
    static Helper helper = new Helper();
  }

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

...