What is singleton class?
Answer Posted / srinivas pentakota
A singleton is an class that can be instantiated once, and
only once. This is a fairly unique property, but useful in a
wide range of object designs. Creating an implementation of
the singleton pattern is fairly straightforward - simple
block off access to all constructors, provide a static
method for getting an instance of the singleton, and prevent
cloning.
public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}
public static SingletonObject getSingletonObject()
{
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;
}
private static SingletonObject ref;
}
| Is This Answer Correct ? | 27 Yes | 14 No |
Post New Answer View All Answers
What is data string?
What is loop in java?
What is better- service oriented or batch oriented solutions?
Give few difference between constructor and method?
What is the difference between choice and list?
What is meant by attribute?
What do you mean by object?
Is array a class?
How many types of java are there?
What is a default constraint?
What is the difference between the ">>" and " >>>" operators in java?
What is heterogeneous in java?
How can we create objects if we make the constructor private ?
What is qualitative variable?
What type of variable is error flag?