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 deadlock? : Java thread
What is protected in java?
Why is java multithreaded?
Why does java have two ways to create child threads?
What is java ceil?
What is the difference between preparedstatement and statement in java?
What about interthread communication and how it takes place in java?
What is size of int in java?
What is the difference between arraylist and hashset in java?
What is difference between printf and scanf?
What is difference between variable declaration and definition?
What is the main use of generics in java?
Which is bigger float or double?
When we should use serialization?
What are the differences between heap and stack memory?