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
What is the difference between private & public & friendly classes?
What is the symbol for line break?
Is string is a keyword in java?
What is bitwise complement?
Is java map thread safe?
What are the benefits of java?
What is boolean example?
Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?
What is heterogeneous in java?
Difference between start() and run() method of thread class?
What is a Presistent Object?
What is the use of coding?
Differentiate between a constructor and a method? Can we mark constructors final?
Is the milky way in a void?
How do you clear an arraylist in java?