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
Why parameters should be passed by reference?
Name some OOPS Concepts in Java?
What is a data structure java?
What value is a variable of the string type automatically initialized?
Differences between external iteration and internal iteration?
Can set contain duplicates?
How we can generate random numbers in java?
Can you override private or static method in java?
What is difference between printf and scanf?
How do you implement singleton class?
What is hashset in java?
Define jre i.e. Java runtime environment?
What is the integer of 16?
What is string literal in java?
What is abstract class? Explain