How to implement or use the singleton class in java?

Answers were Sorted based on User's Feedback



How to implement or use the singleton class in java?..

Answer / amit sharma

singleton is a design pattern. in this per application only
one object should exist.we implement it as follows:

public class abcd
{
private static abcd instance=null;
protected abcd()
{
//only to defeat instantiation;
}
public static abcd getInstance()
{
if(instance==null)
{
instance=new abcd();
}
return instance;
}

Is This Answer Correct ?    11 Yes 4 No

How to implement or use the singleton class in java?..

Answer / 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

How to implement or use the singleton class in java?..

Answer / pinaki mukherjee

A singleton is an object that cannot be instantiated. At
first, that might seem counterintuitive - after all, we need
an instance of an object before we can use it. Well yes a
singleton can be created, but it can't be instantiated by
developers - meaning that the singleton class has control
over how it is created. The restriction on the singleton is
that there can be only one instance of a singleton created
by the Java Virtual Machine (JVM) - by prevent direct
instantiation we can ensure that developers don't create a
second copy.

We'll start with the class definition, for a SingletonObject
class. Next, we provide a default constructor that is marked
as private. No actual code needs to be written, but you're
free to add some initialization code if you'd like.

public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}
}

So far so good. But unless we add some further code,
there'll be absolutely no way to use the class. We want to
prevent direct instantiation, but we still need to allow a
way to get a reference to an instance of the singleton object.

Is This Answer Correct ?    3 Yes 2 No

Post New Answer

More Java J2EE AllOther Interview Questions

where is JNDI being used in java platform?

2 Answers  


How do you debug your java program?

2 Answers   HCL, Inforica,


how to delete cookie information?and when it will delete?

3 Answers   SolutionNET,


What is the enumerator of the java collection framework? : java collections

0 Answers  


How to implement or use the singleton class in java?

3 Answers  






what debugging tool that can be used to debug the java programs?

1 Answers   Inforica,


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

0 Answers  


what is the diffrence between insurance and telecom domain?

1 Answers  


who will give req's to u?. how they send req's to u? . what design documents contains?. when bugs raised on other developer code how to report to them?.(throgh mail or ........). how to retrive 100 recods from dao layer to presentation layer.using which collection?. what is sequence diagram.?.

0 Answers   iGate,


What is the use of hashcode in java ?

0 Answers  


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

0 Answers   Cap Gemini,


what is filter ?

2 Answers   SolutionNET,


Categories