How to prevent Reflection to break a Singleton Class Pattern?


A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −

Example - Breaking Singleton

 Live Demo

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Tester {
   public static void main(String[] args) throws
   InstantiationException, IllegalAccessException,

   IllegalArgumentException, InvocationTargetException{
      A a = A.getInstance();
      A b = null;

      Constructor<?>[] constructors = A.class.getDeclaredConstructors();

      for (Constructor constructor : constructors) {
         //make the private constructor as public
         constructor.setAccessible(true);
         b = (A) constructor.newInstance();
         break;
      }
      System.out.println(a.hashCode());
      System.out.println(b.hashCode());
   }
}

class A implements Serializable {
   private static A a;
   private A(){}

   public static A getInstance(){
      if(a == null){
         a = new A();
      }
      return a;
   }
}

Output

705927765
366712642

Here you can see, we've created another object of a Singleton class. Let's see how to prevent such a situation −

Create A using Enum instead of Class.

Example - Protecting Singleton

 Live Demo

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

public class Tester {
   public static void main(String[] args) throws
   InstantiationException, IllegalAccessException,
   IllegalArgumentException, InvocationTargetException{

      A a = A.INSTANCE;
      A b = A.INSTANCE;

      System.out.println(a.hashCode());
      System.out.println(b.hashCode());
   }
}
enum A implements Serializable {
   INSTANCE;
}

Output

705927765
705927765

Updated on: 23-Jun-2020

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements