How to implement or use the singleton class in java?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
Which server-side script takes the input from JavaScript, can access the database if it needs to, and processes the data.
What are the interfaces in java collections? : java collections
What are the differences between the java collection and the java list? : java collections
what are callback methods?
What are maps interfaces in the java collections? : java collections
lll
Should we create system software ( e.g operating system ) in java ?
when exactly should we use throws ,and wen exactly should we use the try and catch for exceptional handling
How to implement or use the singleton class in java?
waht are architecture of web application?
Write a postfix expression to (a*(b+c/d)*d-e)
what is jndi?