What is singleton class?

Answer Posted / srinivas pentakota

A singleton is an class that can be instantiated once, and
only once. This is a fairly unique property, but useful in a
wide range of object designs. Creating an implementation of
the singleton pattern is fairly straightforward - simple
block off access to all constructors, provide a static
method for getting an instance of the singleton, and prevent
cloning.


public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}

public static SingletonObject getSingletonObject()
{
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;
}

private static SingletonObject ref;
}

Is This Answer Correct ?    27 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of protected in java?

525


What is the difference between yield() and sleep()?

535


What are peerless components in java programming?

573


What is concurrent hashmap and its features?

511


What is the use of string and stringbuffer?

539






What are the important methods of java exception class?

563


What are anonymous inner classes?

557


What is a singleton in genetics?

553


How will you reverse a link list without using recursion?

582


What is integer parseint?

542


Can constructor be synchronized?

527


What is classname class in java?

526


What is java used for on a computer?

497


Difference between method overloading and overriding.

573


What are the concepts of 'OOPS'?

590