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 are peerless components in java programming?

812


What is the difference between public, private, protected, and friend access?

822


What is a qms manual?

788


Why do we use threads in java?

844


What is array class in java?

829


Differentiate between a class and an object.

833


Draw a UML class diagram for the code fragment given below: public class StringApplet extends Applet { private Label sampleString; private Button showTheString; private ButtonHandler bHandler; private FlowLayout layout; public StringApplet() { sampleString = new Label(" "); showTheString = new Button (" Show the String"); bHandler = new ButtonHandler(); layout = new FlowLayout(); showTheString.addActionListener(bHandler); setLayout(layout); add(sampleString); add(showTheString); } class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { samplestring.setText("Good Morning"); } } } Note: The methods need not be indicated on the diagram.

1848


Can a class be a super class and a sub-class at the same time? Give example.

1203


Can you make a constructor final?

816


Is heap stored in ram?

737


What's the base class of all exception classes?

823


What is the final method?

871


What is currentthread()?

798


Do we need to manually write Copy Constructor?

820


What are the important features of Java 10 release?

781