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

Write a postfix expression to (a*(b+c/d)*d-e)

4 Answers   Adobe,


Hi Friends, i have searched in google but not clear. can you give bank example with synchronized keyword

0 Answers   TCS,


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

0 Answers  


what are callback methods?

3 Answers   iFlex,


Which is the better framework for web applications, struts or spring, and why?

1 Answers   IBM, TATA,






HOW MUCH PERCENTAGE WE DEDUCT FOR OUR SUPPLIERS OR OTHER PROFESSIONALS

0 Answers  


where is JNDI being used in java platform?

2 Answers  


When to use the Collection Classes(HashSet, LinkedHashSet, TreeSet....etc) in real time scenario ?

1 Answers   Cap Gemini,


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

3 Answers   SolutionNET,


What is the use of hashcode in java ?

0 Answers  


What are the different types of features of the java collections framework? : java collections

0 Answers  


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

0 Answers  


Categories