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 are the different types of inner classes?
What is the difference between the size and capacity of a vector?
How will you serialize a singleton class without violating singleton pattern?
What do you understand by classes in java?
Difference between linkedlist and arraylist.
What is the difference between super class & sub class?
How do you do absolute value in java?
What are recursive functions?
Which methods cannot be overridden in java?
In java how do we copy objects?
What is a conditional statement explain with example?
how does the run() method in runnable work? : Java thread
What type of language is java?
Why do we use string?
How do you sort an array in java?