package com.michaelthomas.singleton; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * * Note: Original concepts came from Wikipedia.com * http://en.wikipedia.org/wiki/Singleton_pattern */ public class SingletonLazyInnerClassExample { /* * The "On Demand" (aka "Lazy") way to create a Singleton. The INSTANCE is * created the first time getInstance() is called. This solution is * thread-safe without requiring special language constructs (i.e. volatile * or synchronized). Designed by: Bill Pugh from the University of Maryland * because of issues in the Java memory model prior to Java 5. */ private String name; // Private constructor prevents instantiation from other classes private SingletonLazyInnerClassExample() { } /** * SingletonHolder is loaded on the first execution of * Singleton.getInstance() or the first access to SingletonHolder.INSTANCE, * not before. */ private static class SingletonHolder { public static final SingletonLazyInnerClassExample instance = new SingletonLazyInnerClassExample(); } public static SingletonLazyInnerClassExample getInstance() { return SingletonHolder.instance; } public void setName(String name) { this.name = name; } public String getName() { return name; } }