Friday 14 October 2011

Java Singleton Objects

Here's the final source code for a SingletonObject, which you can use as a template for your own singletons.
public class SingletonObject
{
  private SingletonObject()
  {
  }

  public static SingletonObject getSingletonObject()
  {
    if (ref == null)
    ref = new SingletonObject();  
    return ref;
  }

  public Object clone()
 throws CloneNotSupportedException
  {
    throw new CloneNotSupportedException(); 
  }

  private static SingletonObject ref;
}