GeekInterview.com
Series: Subject: Topic:
Question: 142 of 177

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

Asked by: Interview Candidate | Asked on: Sep 8th, 2005
Showing Answers 1 - 13 of 13 Answers
senthilkumar

Answered On : 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.

  
Login to rate this answer.
Anil Das

Answered On : 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.

  
Login to rate this answer.
veeru

Answered On : 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. 
 

Yes  1 User has rated as useful.
  
Login to rate this answer.
Bhupender Giri

Answered On : 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();
 }
 
}

  
Login to rate this answer.
javamatrix

Answered On : Nov 9th, 2005

View all answers by javamatrix

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

  
Login to rate this answer.
Aparna

Answered On : 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

  
Login to rate this answer.
rajendra

Answered On : 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

  
Login to rate this answer.
SAGAR

Answered On : 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);
 }
 
}

  
Login to rate this answer.
Vijay

Answered On : 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

  
Login to rate this answer.
Meena

Answered On : Sep 11th, 2007

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

  
Login to rate this answer.
sbasani

Answered On : Jan 29th, 2008

View all answers by sbasani

If I am not wrong, we could create object by using cloning concept????

  
Login to rate this answer.
sampra

Answered On : Mar 4th, 2008

View all answers by sampra

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.

  
Login to rate this answer.

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.