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 a null check?

514


Explain java coding standards for variables ?

639


Why set is used in java?

545


What are adapter classes?

582


What is double in java?

493






Objects or references which of them gets garbage collected?

507


Explain the features of interfaces in java?

534


Why declare Main() method as a static in java ?

571


What access modifiers can be used for methods?

533


how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? Or extending thread class or implementing runnable interface. Which is better? : Java thread

571


Explain about fail safe iterators in java?

541


Explain what do you mean by functional overloading in java?

535


why we use merge option in hybernate pls give a ex snippet

1434


What is the most important feature of java? What is an interface?

493


What are the supported platforms by java programming language?

540