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 string is immutable?

527


What are extraneous variables examples?

539


What are abstract methods in java?

658


How we can declare a static variable?

658


Explain the difference between abstract class and interface in java?

541






Can you pass functions in java?

578


Is set ordered in java?

576


Can we extend immutable class?

541


What are methods and how are they defined?

607


What is the purpose of java?

564


What does s mean in regex?

557


How do you use compareto method?

544


How thread scheduler schedule the task?

593


What is a protected void?

512


What is a wrapper method?

541