What is singleton class & it's implementation.

Showing Answers 1 - 5 of 5 Answers

Vinoth Senthill

  • Jul 1st, 2005
 

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..

  Was this answer useful?  Yes

Hemanth Kumar Venugobal

  • Oct 18th, 2005
 

good

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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");
  }
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions