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
Can we have more than one package statement in source file ?
What is * argv?
Is break statement can be used as labels in java?
What is compareto?
is it possible to instantiate the math class?
What is lazy initialization in java?
How do you use compareto method?
What are the drawbacks for singleton class?
How do you create a null object?
What is the benefit of inner / nested classes ?
How does indexof work?
What is collection api?
Is sizeof a keyword in java programming?
Can an arraylist be empty?
Why Java is not pure Object Oriented language?