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
Which variables are stored in heap?
What is udp in java?
When will you define a method as static in Java?
What class allows you to read objects directly from a stream in java programming?
What is use of map in java?
Is assembly language a low level language?
What is queue in java?
How to overcome the exception object reference not set to an instance of object?
How can I become a good programmer?
Is a char always 1 byte?
Differentiate between run time error and syntax error.
which pattern is default in scanner package?
Will the jvm load the package twice at runtime?
explain the difference between jdk and jvm?
Why string is called as immutable?