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
What is Applet Stub Interface ?
Difference between this() and super() in java ?
What is a treeset class?
What is variable and constant explain with example?
What is meant by bytecode?
Can we instantiate interface in java?
Explain about arraylist?
What is arraylist e?
What are the 6 functions?
what is mutual exclusion? How can you take care of mutual exclusion using java threads? : Java thread
Define max and min heap, also the search time of heap.
Why can't we make a class private in java?
How does java enable high performance?
Differentiate between static and non-static methods in java.
Explain the protected field modifier?