What is a Singleton class. How do you write it ?

Showing Answers 1 - 17 of 17 Answers

senthilkumar

  • Sep 6th, 2005
 

singleton class is used to create only one instance of that class that is using static concept.if we create a object of that class means recently created instance will come to effect.example is connection.

  Was this answer useful?  Yes

Anil Das

  • Sep 6th, 2005
 

Singleton class can be created by defining the class static and making its constructor private so that no other class can user the new() keyword to create the instance of the singleton class. It will provide a public method which will create or return the already created singleton class object.

  Was this answer useful?  Yes

veeru

  • Sep 8th, 2005
 

SingleTon class : is a special class which provides only one instance. 
Ex:Container Developers are using this class. 
 
Code is like this: 
 
//This is Single ton class 
public class SingleTon 

private static SingleTon ton = null; 
public static SingleTon getName() 

if(ton == null) 

ton = new SingleTon(); 
return ton;  


 
 
//tnis outside class where you can call getName() method but you will get same Instance 
 
public class TonInsatnce 

public static void main(String args[]) 

Object object = SingleTon.getName(); 


 
I have tried my level if any suggestion plz,intimate. 
 

Bhupender Giri

  • Nov 9th, 2005
 

I m writinga a code which will ensure that there will be only one instance of that class.Just check it.

class singletonClass{
 
 public singletonClass sc=null;
 
 private singletonClass(){
  System.out.println("In Constructor");
   
 }
 
 public singletonClass getInstance(){
  if(sc==null){
   System.out.println("Creating first instance " );
   sc= new singletonClass();
  } 
  return sc;
 }
 
 
 
}

public class singletonClassMain(){

 public static void main(String args[]){
  singletonClass scm = new singletonClass();
  scm=singletonClass.getInstance();
 }
 
}

  Was this answer useful?  Yes

javamatrix

  • Nov 9th, 2005
 

Anil Das Wrote: Singleton class can be created by defining the class static and making its constructor private so that no other class can user the new() keyword to create the instance of the singleton class. It will provide a public method which will create or return the already created singleton class object.

how can u have a static class ????

  Was this answer useful?  Yes

Aparna

  • Mar 31st, 2006
 

HI I tried this code.. but not working successfully.

and you shouldnot use two public classes in the same java file.

aparna

  Was this answer useful?  Yes

rajendra

  • Apr 21st, 2006
 

hi

how do we check whether ths class is single ton or not.

if we check by hashcode what will be the hashcode of all the singleton objects is it same or not.

regards

rajendra

  Was this answer useful?  Yes

SAGAR

  • Apr 26th, 2006
 

I think the above code contains some modification for the above code.

I executed this and I am getting the same references for the scm and dcm objects.

If i am not wrong, I am expecting this as solution

class singletonClass{
 
 public static singletonClass sc=null;
 
 private singletonClass(){
  System.out.println("In Constructor");
  
 }
 
 public static singletonClass getInstance(){
  if(sc==null){
   System.out.println("Creating first instance " );
   sc= new singletonClass();
  }
  return sc;
 }
 
 
 
}

public class singletonClassMain{

 public static void main(String args[]){
  //singletonClass scm = new singletonClass();
  singletonClass scm=singletonClass.getInstance();
  System.out.println(scm);
    singletonClass dcm=singletonClass.getInstance();
   System.out.println(scm);
     System.out.println(dcm);
 }
 
}

  Was this answer useful?  Yes

Vijay

  • Jun 22nd, 2006
 

Singleton Is a design pattern which is used to restrict a class to have a single object. i.e at a time a class can instantiate only a single object.You can take the code from above just wanted to let you know that singleton is a design pattern

  Was this answer useful?  Yes

Meena

  • Sep 11th, 2007
 

In the getName(), the return statement should be outside the if statement.

  Was this answer useful?  Yes

sampra

  • Mar 4th, 2008
 

Singleton class can be created by defining the class static and making its constructor private so that no other class can user the new() keyword to create the instance of the singleton class. It will provide a public method which will create or return the already created singleton class object.

  Was this answer useful?  Yes

For every Servlet there will be only one Servlet Object, if at all multiple requests are arriving then the existing Servlet Object vl be retrieved(i.e a new Servlet Object is not created)
internally web servers are using SingleTon Design pattern...........

eg:Apache Tomcat Server.

  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