With the Singleton design pattern you can: Ensure that only one instance of a class is created Provide a global point of access to the object Allow multiple instances in the future without affecting a singleton class's clients
//Sample Code public class SingleInstance { private static SingleInstance ourInstance = new SingleInstance();
public static SingleInstance getInstance() { if(null==ourInstance){ ourInstance= new SingleInstance(); } return ourInstance; } private SingleInstance() { } }
Rose
Sep 8th, 2005
Nice explanation.... usage of singleton classes can also be included..
Hemanth Kumar Venugobal
Oct 18th, 2005
good
artika
Sep 12th, 2006
when only one instance of a class can be made
then wat's the purpose of makin it as a class
chandrakanth
Sep 27th, 2006
In singleton class ,only one time the instance wil create.Example....
public class Sin { static Sin s=null; public static void main(String a[]) { create(); create(); create(); } public static Sin create() { if(s==null) { s=new Sin(); } else { System.out.println("Please it is a single ton class"); } return s; } private Sin() { System.out.println("Single ton class"); } }
What is singleton class & it's implementation.