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
Define jit compiler?
What is array pointers ?
How can you set the applet size?
How do you access command-line arguments within the code?
Why inputstreamreader is used in java?
What is the mapping mechanism used by java to identify IDL language?
Does windows 10 need java?
How do you define a method?
What will happen when using pass by reference in java?
Differences between external iteration and internal iteration?
How do you do a line break in java?
What is the hashcode () and equals () used for?
Is empty in java?
If a class is declared without any access modifiers, where may the class be accessed in java programming?
why java uses class level type casting ?