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

Explain inner classes ?

886


How do you stop a thread in java?

828


List types of storage classes in java?

843


Can a class extend more than one class?

864


How do you input a string in java?

761


How to declare an arraylist in java?

716


What if I write static public void instead of public static void in java?

837


What is jee6?

763


What is string and example?

793


How do weakhashmap works?

856


Write a program to reverse a number in java?

804


What is a parameter in matrices?

771


Which sorting algorithm is in place?

736


What do you know about java?

736


What does n mean?

722