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 isan abstract class and when do you use it?
What does serializing data mean?
What is indexof in java?
Is static variable stored in heap?
what do you understand by synchronization? Or what is synchronization and why is it important? Or describe synchronization in respect to multithreading? Or what is synchronization? : Java thread
What is the byte order of byte buffer?
What is the difference between java applets and applications?
how to write a program for sending mails between client and server
What if the main() method is declared as private? What happens when the static modifier is removed from the signature of the main() method?
What are the procedures?
Can we modify the throws clause of the superclass method while overriding it in the subclass?
How do you check if two strings are equal in java?
what is use of functional interface in java 8?
What are variable arguments or varargs?
What is ellipsis in java?