How to implement Singleton

Answer Posted / hardeep thakur

Here is code to implement the singleton

public class SingletonClass {
private static SingletonClass singleObj ;
private SingletonClass(){}

public static synchronized SingletonClass getInstance()
{
if(singleObj ==null){
singleObj = new SingletonClass();
}
return singleObj;
}

public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}

}

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you add an element to a set in java?

770


What are the legal operands of the instanceof operator?

787


Can we catch more than one exception in single catch block?

843


Explain the scope of a variable.

845


Is double bigger than float?

749


Can constructor return value?

739


List types of storage classes in java?

833


Assume a thread has lock on it, calling sleep() method on that thread will release the lock?

878


What is final int?

723


Explain about features of local inner class?

825


Differentiate between stringbuffer and string?

796


What is difference between == equals () and compareto () method?

740


What is jar?

847


Can we have a method name same as class name in java?

815


How do you initialize an arraylist in java?

741