Pages

Java

Singleton Class

public class SingletonDemo{
 
    private static SingletonDemo instance;
 
    /** A private Constructor prevents any other class from instantiating. */
    private SingletonDemo() {}
 
    public static synchronized SingletonObject getInstance() {
      if instance == null){
          instance = new SingletonDemo();
      }
 
 return instance;
 
    }
 
    public Object clone() throws CloneNotSupportedException{
 throw new CloneNotSupportedException();
    }
}

No comments:

Post a Comment