What is singleton class?

Answer Posted / ranganathkini

A Singleton is a class which at any given time has only one
instance and it does not allow more instances to be created.
Such classes do not have an accessible constructor, for example:

public class Singleton {
// class is automatically instantiated when the
// class is loaded
private static Singleton instance = new Singleton()

// constructor is made inaccessible by declaring
// it private
private Singleton() { ... }

// Access to the single instance of the class is
// provided by a static accessor method
public static Singleton getInstance() {
// returns a reference of the private instance
return instance;
}

// rest of the class implementation
}

Is This Answer Correct ?    176 Yes 21 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is meant by javabeans?

610


When do we use synchronized methods in java?

580


What is string english?

533


How many types of design patterns are there?

524


What are accessor methods in java?

546






How do you check if a string is lexicographically in java?

496


Do you know how to reverse string in java?

578


What happens if we don’t override run method ?

560


What is a java object and java application?

573


What does exp mean in math?

528


Is cout buffered?

682


How do you do math powers in java?

570


How to find the largest value from the given array.

522


What is a qms manual?

538


Explain public static void main(string args[]) in java.

526