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

What are different types of arrays?

537


When should the method invokelater() be used?

584


What is a void method java?

520


Can a class be protected in java?

506


Can we call the constructor of a class more than once for an object?

635






What is an array in java?

640


What is the finalize method do?

604


Why do we need array in java?

534


What are adapter classes?

601


What is the difference between a scrollbar and a scrollpane?

565


what is the purpose of the runtime class?

555


Write a java program to print fibonacci series?

542


What is method overriding in java ?

664


What is jpa specification?

539


what are three ways in which a thread can enter the waiting state? Or what are different ways in which a thread can enter the waiting state? : Java thread

525