title: Java singleton deserialization values keep default
body: <p>I have a simple singleton:</p>
  
  <pre><code>class Test1 implements Serializable {
      private static Test1 instance;
  
      public String a = "a";
      public String b = "b";
      public String c = null;
      public String d = null;
      public String e;
      public String f;
  
      private Test1() {
          e = "e";
      }
  
      public static Test1 getInstance() {
          if (instance == null) {
              instance = new Test1();
          }
          return instance;
      }
  
      // http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-with-examples
      protected Object readResolve() {
          return getInstance();
      }
  
      public String toString() {
          return String.format("Test1{ a:%s, b:%s, c:%s, d:%s, e:%s, f:%s}", a, b, c, d, e, f);
      }
  }
  </code></pre>
  
  <p>My <code>main()</code>:</p>
  
  <pre><code>if ((new File("t1.obj").exists() == false)) {
      Test1 t1 = Test1.getInstance();
      t1.b = "bb";
      t1.d = "dd";
      t1.f = "ff";
  
      serialize("t1.obj", t1);
  }
  
  else {
      Test1 t2 = deserialize("t1.obj");
  }
  </code></pre>
  
  <p>First run looks well</p>
  
  <pre><code>Serialized hu.fehergeri13.abptc.server.Test1 object to t1.obj file.
  Test1{ a:a, b:bb, c:null, d:dd, e:e, f:ff}
  </code></pre>
  
  <p>after second run:</p>
  
  <pre><code>Deserialized hu.fehergeri13.abptc.server.Test1 object from t1.obj file.
  Test1{ a:a, b:b, c:null, d:null, e:e, f:null}
  </code></pre>
  
  <p>My serialize/deserialize:</p>
  
  <pre><code>public static void serialize(String filePath, Object o) {
      try {
          FileOutputStream fos = new FileOutputStream(filePath);
          ObjectOutputStream oos = new ObjectOutputStream(fos);
          oos.writeObject(o);
  
          System.out.println(String.format("Serialized %s object to %s file.", o.getClass().getName(), filePath));
          oos.close();
          fos.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public static &lt;T&gt; T deserialize(String filePath) {
      try {
          FileInputStream is = new FileInputStream(filePath);
          ObjectInputStream ois = new ObjectInputStream(is);
          T o = (T) ois.readObject();
  
          System.out.println(String.format("Deserialized %s object from %s file.", o.getClass().getName(), filePath));
          ois.close();
          is.close();
          return o;
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>Why are the values keep default and not <code>bb</code>,<code>dd</code>,<code>ff</code>?</p>