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


Please Help Members By Posting Answers For Below Questions

Can we have more than one package statement in source file ?

580


What is * argv?

696


Is break statement can be used as labels in java?

495


What is compareto?

533


is it possible to instantiate the math class?

520






What is lazy initialization in java?

543


How do you use compareto method?

523


What are the drawbacks for singleton class?

510


How do you create a null object?

499


What is the benefit of inner / nested classes ?

519


How does indexof work?

498


What is collection api?

601


Is sizeof a keyword in java programming?

597


Can an arraylist be empty?

556


Why Java is not pure Object Oriented language?

633