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 the differences between forwarding () method and sendredirect() methods?

615


Add a value x to array from index l to r where 0 <= l <= r <= n-1

621


What is the use of arraylist in java?

521


What is the old name of java?

499


What does  mean in regex?

565






What is the difference between assignment and initialization?

550


What do you understand by a Static Variable?

624


What is early binding and late binding in java?

591


What are the advantages of unicode?

541


Can a class have multiple constructors?

531


What is use of set in java?

486


Why are the methods of the math class static?

574


Can we force the garbage collection to run?

531


Difference between final and effectively final ? Why is effectively final even required ?

548


What is a war file?

544