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

What is the biggest integer?

826


What is the full meaning of java?

810


What is a lightweight component?

818


Write a program to search a number in the given list of numbers.

849


What is the difference between multiple processes and multiple threads?

817


What do you mean by thread safe?

785


Is list ordered in java?

794


Is arraylist ordered?

804


What are the different approaches to implement a function to generate a random number?

825


How is a structure different from array ?

811


What are the types of methods in java?

806


What is the reason behind using constructors and destructors?

813


What is a null point?

767


What is a conditional statement explain with example?

781


When should I use singleton?

720