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 do you mean by platform independence? What is an interface?

756


Can we force the garbage collection to run?

696


how can you catch multiple exceptions in java?

721


What is the difference between declaration and definition in java?

709


Considering notepad/ie or any other thing as process, what will happen if you start notepad or ie 3 times? Where 3 processes are started or 3 threads are started?

864






What is primitive array?

665


Can you pass functions in java?

759


What is serial version uid and its importance in java?

782


What is local declaration?

677


Which container method is used to cause a container to be laid out and redisplayed in java programming?

810


What are byte codes?

832


Which is better list or arraylist in java?

635


What are abstract classes and anonymous classes?

817


What is int short for?

699


What is enhanced loop in java?

711