How to implement or use the singleton class in java?

Answer Posted / aashish r. wadhokar

Singleton is a design pattern as well as class.
when we define singleton class it means we can have only one
object of that class.
We cant create multiple objects of that class.
In singleton class, constructor is always private.

eg:
public class Singleton
{
private static Singleton singletonObject = null;
private Singleton()
{
System.out.println("Singleton object created!!!");
}
public static Singleton CreateInstance()
{
if(singletonObject == null)
{
singletonObject = new Singleton();
}
return singletonObject;
}

Now we can get object as:
Singleton ref = null;

ref = Singleton.CreateInstance();

Is This Answer Correct ?    6 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the uses of the set interfaces in the java collections? : java collections

534


Hi frnds how to lock an user when he enter wrong credentials more than 3 time using java or j2ee tech take username and password in a bean no need to connect DB and validate give me some sample application code or links its urgent for me thanks in advance

1869


What are the different types of ways where you can iterate over a list? : java collections

572


what is mean by hasing and maping in java platform and advantage?

1827


What are the different types of classes implemented in the set interfaces? : java collections

602






What are the main classes of the list interfaces? : java collections

574


please mail me the interview question based on java/j2ee

1530


Explain the OOPS concept in Realtime Scenarion ? Take example as CAR. Please explain indetail ?

2561


Which sorting algorithm is used by collections.sort() in java ?

545


What are the queues in the java collection framework? : java collections

546


What are the different types of collections views being provided by the map interface? : java collections

613


Should we create system software ( e.g operating system ) in java ?

574


Hi we have an urgent requirement for Java/J2ee technical lead position & also looking for "Java Guidewire claimcentre" experienced professional for Bangalore location if interested can reach srisanh@gmail.com

2534


Hi Friends , am newbie to ajax. For example just consider one account registration - A form contains 8 text fields with submit button. In this form second texbox contains "username " . On right side of text box there is a label box . On clilck action i need to determine user is available or not. Is it possible on clicking label or should i click submit button.

1581


What is iterator in the java collections framework? : java collections

533