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 the difference between private & public & friendly classes?

752


What is the symbol for line break?

807


Is string is a keyword in java?

721


What is bitwise complement?

699


Is java map thread safe?

684






What are the benefits of java?

755


What is boolean example?

708


Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?

1240


What is heterogeneous in java?

690


Difference between start() and run() method of thread class?

780


What is a Presistent Object?

888


What is the use of coding?

707


Differentiate between a constructor and a method? Can we mark constructors final?

908


Is the milky way in a void?

723


How do you clear an arraylist in java?

680