top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we clone singleton class in Java?

+1 vote
1,022 views
Can we clone singleton class in Java?
posted Aug 16, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Not if it is really a singleton.

SingletonObject clone = (SingletonObject) obj.clone();

That throws a CloneNotSupportedException if the singleton class doesn't implement the Cloneable interface.

Cloning a Singleton class sample code

Cloning Singleton class

We can clone a Singleton class instance in a scenario where the singleton class extends from a class which implements Cloneable interface and provides implementation of clone() method. So now we can clone the instance by calling the Object class's clone() method on the singleton instance.

class SingletonSuper implements Cloneable {  
         public Object clone() throws CloneNotSupportedException {  
                 return super.clone();  
         }  
 }  
 class Singleton extends SingletonSuper {  
          // 1. Make all constructors private  
          private Singleton() {}  
          // 2.   Declare a private static variable to hold single instance of class  
          private static Singleton INSTANCE = new Singleton();  
          // 3.   Declare a public static function that returns the single instance of class   
          public static Singleton getInstance() {  
                 return INSTANCE;  
          }  
 }  
 public class SingletonCloningTest  
 {  
         public static void main(String[] args) throws Exception  
         {  
                 System.out.println("Singleton Test!");  
                 System.out.println("Singleton Instance:"+Singleton.getInstance());  
                 System.out.println("Singleton clone:"+Singleton.getInstance().clone());  
         }  
 }  

Prevent cloning of singleton class

We can override the Object class's clone() method to throw the CloneNotSupportedException exception.

 class SingletonSuper implements Cloneable {  
         public Object clone() throws CloneNotSupportedException {  
                 return super.clone();  
         }  
 }  
 class Singleton extends SingletonSuper {  
          // 1. Make all constructors private  
          private Singleton() {}  
          // 2.   Declare a private static variable to hold single instance of class  
          private static Singleton INSTANCE = new Singleton();  
          // 3.   Declare a public static function that returns the single instance of class   
          public static Singleton getInstance() {  
                 return INSTANCE;  
          }  
          public Object clone() throws CloneNotSupportedException {  
                 // throw CloneNotSupportedException if someone tries to clone the singleton object  
                 throw new CloneNotSupportedException();  
          }  
 }  
 public class SingletonPreventCloningTest  
 {  
         public static void main(String[] args) throws Exception  
         {  
                 System.out.println("Singleton Test!");  
                 System.out.println("Singleton Instance:"+Singleton.getInstance());  
                 // will throw exception if clone method is called  
                 System.out.println("Singleton clone:"+Singleton.getInstance().clone());  
         }  
 }  
answer Aug 17, 2015 by Karthick.c
...