package com.michaelthomas.singleton; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * */ public class SingletonLazyExample { /* * The "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). */ private String name; private static SingletonLazyExample instance; // Private constructor prevents instantiation from other classes private SingletonLazyExample() { } public static SingletonLazyExample getInstance() { if (instance == null) { instance = new SingletonLazyExample(); } return instance; } public void setName(String name) { this.name = name; } public String getName() { return name; } }