title: Implementation of readResolve() method for Serializable Singleton Instance
body: <p>I am trying to write a Serializable Singleton class by adding the readResolve() method. My intention is to get the same object with its object's state at the time of serialization time.</p>
  <p>below is my Test example code:</p>
  <pre><code>import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  
  public class SingletonDemo {
  
      public static void main(String[] args) {
            Singleton obj = Singleton.getInstance();
            System.out.println(&quot;After NEW Object creation : &quot; + obj);
  
            obj.i = 5;
            System.out.println(&quot;Object modified&quot;);
            System.out.println(&quot;After Object 1st Modification : &quot; + obj);
  
            serializeMe();
            System.out.println(&quot;Serialized successfully with object state : &quot; + obj);
  
            obj.i = 10;
            System.out.println(&quot;Object modified again&quot;);
            System.out.println(&quot;After Object 2nd Modification : &quot; + obj);
  
            Singleton st = (Singleton)deSerializeMe();
            System.out.println(&quot;Deserialized successfully&quot;);
  
            System.out.println(&quot;After Deserialization : &quot; + st);
      }
      
      public static void serializeMe() {
      ObjectOutputStream oos = null;
  
      try {
          oos = new ObjectOutputStream(new FileOutputStream(&quot;d:\\SingletonData.txt&quot;));
          oos.writeObject(MySerializedSingleton.getInstance());
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public static Object deSerializeMe() {
      ObjectInputStream oin = null;
      Object obj = null;
  
      try {
          oin = new ObjectInputStream(new FileInputStream(&quot;d:\\SingletonData.txt&quot;));
          obj = oin.readObject();
      } catch (IOException | ClassNotFoundException e) {
          e.printStackTrace();
  
      }
      return obj;
  }
  </code></pre>
  <p>}</p>
  <pre><code>class Singleton implements Serializable {
     int i;
     private static Singleton obj = null;
  
     private Singleton() {
        System.out.println(&quot;Executing constructor&quot;);
        i=1;
     }
  
     public static Singleton getInstance() {
        if(obj == null) {
           obj = new Singleton();
        }
        System.out.println(&quot;An instance is returned&quot;);
        return obj;
     }
  
     /*private void writeObject(ObjectOutputStream oos) {
     try {
         oos.writeInt(i);
     } catch (Exception e) {
         e.printStackTrace();
     }
     }
  
     private void readObject(ObjectInputStream ois) {
     try {
         i = ois.readInt();
     } catch (Exception e) {
         e.printStackTrace();
     }
     }*/
  
     public Object readResolve() {
        System.out.println(&quot;Executing readResolve&quot;);
        return Singleton.getInstance(); // FIXME
     }
  
     @Override
     public String toString() {
        return &quot;Singleton [i=&quot; + i + &quot;]&quot;;
     }
  }
  </code></pre>
  <p>OUTPUT:</p>
  <pre><code>Executing constructor
  An instance is returned
  After NEW Object creation : Singleton [i=1]
  Object modified
  After Object 1st Modification : Singleton [i=5]
  An instance is returned
  Serialized successfully with object state : Singleton [i=5]
  Object modified again
  After Object 2nd Modification : Singleton [i=10]
  Executing readResolve
  An instance is returned
  Deserialized successfully
  After Deserialization : Singleton [i=10]
  </code></pre>
  <p>I know the current scenario will always return the same instance of Singleton with the latest Object's state.</p>
  <p>I tried overriding the writeObject() and readObject() (commented in this above code) but not getting the desired result.
  i.e.</p>
  <pre><code>After Deserialization : Singleton [i=5]
  </code></pre>
  <p>But there is no reference of ObjectInputStream in readResolve() so that I can get the instance and update it with the serialized object's state before returning.</p>
  <p>Please correct me if I am wrong in my conception and help me to solve this.</p>
  <p>Thanks.</p>