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 do you understand by the term string pool?

561


Compare overloading and overriding?

557


What are annotations in java?

619


Explain about class in java?

608


How do you use compareto method?

529






What is a predicate method?

560


Is it possible to instantiate the abstract class?

538


What is string substring?

559


What is thread count in java?

543


What is a arraylist in java?

553


Difference between a class and an object?

572


Write a program to calculate factorial in java?

580


What is toarray method in java?

556


What is the difference between length and size in java?

493


What is the purpose of nested class in java?

599