What is singleton class?
Answer Posted / marshallsudhan
singleton class - has single Instance.
The Singleton is a useful Design Pattern for allowing only
one instance of your class, but common mistakes can
inadvertently allow more than one instance to be created.
The Singleton's purpose is to control object creation,
limiting the number to one but allowing the flexibility to
create more objects if the situation changes. Since there
is only one Singleton instance, any instance fields of a
Singleton will occur only once per class, just like static
fields.
//Eg Pgm.
class Sample
{
void m1()
{
System.out.println("Method m1");
}
void m2()
{
System.out.println("Method m2");
}
private Sample()
{
System.out.println("Constructor");
}
public static void main(String[] args)
{
Sample s = new Sample();
s.m1();
s.m2();
}
| Is This Answer Correct ? | 49 Yes | 74 No |
Post New Answer View All Answers
Explain creating threads by implementing runnable class?
what state does a thread enter when it terminates its processing? : Java thread
What is a final class in java?
Detail discussions on JVM, memory management and garbage collector.
Explain the difference between treeset and treemap in java?
Write code of any action class?
What two classes are used to read data only?
Can you tell me range of byte?
When a thread is executing a synchronized method , then is it possible for the same thread to access other synchronized methods of an object ?
How list contains works in java?
What is Garbage Collection in Java
What is the difference between length and length () in java?
What do you understand by the term singleton?
Can we override data members in java?
A person says that he compiled a java class successfully without even having a main method in it? Is it possible?