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 are synchronized blocks in java?

596


Write a program based on Java script program.

637


What is the vector class in java programming?

556


What is scope of a variable?

595


Is java based on c?

524






How is a structure different from array ?

572


How do you access command-line arguments within the code?

568


What are the library functions in java?

542


if u open login & logout ,how can udisplay the timelogin & logout members ?

1746


How are the elements of a gridbaglayout organized?

539


Can we change the value of static variable?

486


What happens when I use / and % with a negative numerator?

532


Where to store local variables?

575


what is thread in Java ?

618


What is the impact of declaring a method as final?

555