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
What do you mean by constant time complexity?
What is the driver class?
List primitive java types?
How are this() and super() used with constructors in java programming?
What means public static?
Is it possible to use Semaphore/ Mutex in an Interrupt Handler?
Explain with example the concept of constant variable in java.
What is the static block?
How do you add an arraylist to an array in java?
What are the methods to rectify ambiguities in the interfaces in JAVA?
How many types of equations are there?
Is zero a positive integer?
Explain importance of finally block in java?
What is subsequence of a string?
Can we split string with in java?