What is a Singleton class, what is it\'s significance? Thanks in advance!

Questions by sunil_dalvi82   answers by sunil_dalvi82

Showing Answers 1 - 9 of 9 Answers

M Manish

  • Feb 13th, 2006
 

Hi,

Singleton Design pattern helps us to have only one instance of a class in the entire JVM(for a class-loader) and provides global one-point access to it.

There are several ways to achieve this:

1.

public class Singleton

{

  Was this answer useful?  Yes

Manish

  • Feb 13th, 2006
 

Sorry, the post went wrongly

To continue...

public class Singleton{

private static Singleton uniqueInstance;

/*Make a private Contructor. This would ensure that no-one can make an instance of Singleton from outside*/

private Singleton(){}

public static synchronized SingletongetInstance()

{

    if(uniqueInstance==null)

{

    uniqueInstance=new Singleton();

   }

   return uniqueInstance;

   }

//your normal methods go here...

}

2. Make an instance of Singleton when the JVM loads the class:

public class Singleton

{

  private static Singleton uniqueInstance=new Singleton();

  private Singleton(){}

  public static Singleton getInstance()

  {

     return uniqueInstance;

  }

}

I believe this should help you to create a Singleton class.

  Was this answer useful?  Yes

Srihari

  • Feb 14th, 2006
 

Singleton is one and only one instance of a Class for a class loader, which is used by all users of application.

Public class Singleton {

  Singleton singleton = null;

  private Singleton () {} // Inportant 

  public static Singleton synchronized getSingletonInstance() {

      if(singleton ==null )

  }

}

  Was this answer useful?  Yes

JavaProgramer

  • Feb 14th, 2006
 

Singleton design pattern is used to make one and only one instance of a specific class, which can be used by all users of the application.

Public Class Singleton {

  private static Singleton singleton = null; 

  Private Singleton(){} //Important

  public static Singleton synchronized getSingletonInstance() {

    if(singleton ==nul)

      singleton = new Singleton();

    return singleton;

  }

  public void businessMethod() {

     /*

      Write any Business logic here

    */

  }

   public Object clone() throws CloneNotSupportedException {

    raise new CloneNotSupportedException();
    }

}

  Was this answer useful?  Yes

When developer of the class doesn't want to allow the user to create more than one object he will create class such a way that no one can create more than one object of that class

class SingalTone{
private static SingalTone st;
private SingalTone(){
 System.out.println("SingalTone object is created !!");
}
static{
 st = new SingalTone();
}
public static SingalTone createObject(){
 return st;
}
}
public class STTest{
public static void main(String args[]){
 SingalTone st1,st2,st3;
 st1 = SingalTone.createObject();
 st2 = SingalTone.createObject();
 st3 = SingalTone.createObject();
 System.out.println("st1.hashCode() --> "+st1.hashCode());
 System.out.println("st2.hashCode() --> "+st2.hashCode());
 System.out.println("st3.hashCode() --> "+st3.hashCode());
 }
}

  Was this answer useful?  Yes

Shahid mohammed

  • Mar 19th, 2006
 

If we want to give security or if we want the only one instance of a class must be created then we go for singletonobject.

  Was this answer useful?  Yes

Guest

  • Mar 22nd, 2006
 

Hi All,

Generally Singleton Pattern is used while accessing database related objects. It provides one point access to whole java application. It makes sense to make it thread safe by use of synchronized keyword.

Regards

-ashruf

  Was this answer useful?  Yes

rams_k

  • May 10th, 2006
 

if we dont wnat a class to be subclassed or only one obj to be created go for singletone it only allows only one obj to be createdand rest of them to use that obj for example

Public Class Singleton {

  private static Singleton singleton = null; 

  Private Singleton(){} //Important

  public static Singleton synchronized getSingletonInstance() {

    if(singleton ==nul)

      singleton = new Singleton();

    return singleton;

  }

so here we cant access aprivate constructor if there is obj it returns that else creates a new one as we r declaring class as public we can call that class and instance method is static we can call directly without obj.The singletone will b used mainly when only one obj to b created for appl,for ex if we want only one database connection for our appl go for the singletone

i think iam correct...i welcome my mistakes and guys help me out if iam wrong

contact me @ ramu.konkimalla@gmail.com

  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