title: How to automatically add new variables to files in Java?
body: <p>So I have this class.</p>
  <pre><code>public class Player implements Serializable {
      private static final long serialVersionUID = 1L;
      
      public int    health;
      public String name;
  }
  </code></pre>
  <p>I load the Player when they &quot;connect&quot;, and I save the Player when they &quot;Disconnect&quot;.
  I save and load using the Object Input/Output stream.</p>
  <p>Later on, I want to add a gold variable to the Player.
  So the code will be:</p>
  <pre><code>public class Player implements Serializable {
      private static final long serialVersionUID = 1L;
      
      public int    health;
      public String name;
      public int    gold;
  }
  </code></pre>
  <p>I still want the file to contain the health and name, but I want to update it with the gold variable; how would I go about doing that?
  I know I can check if the gold is null, and if so append it to the next save. But is there a way where I can add as many variables as I will, and the code will automatically save the new variables in the class?</p>
  <p>Is there a way for the code to automatically add new variables to the file?</p>
  <p>Load Player:</p>
  <pre><code>try {
      final FileInputStream   fileInputStream   = new FileInputStream(file);
      final ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
      try {
          user = (User) objectInputStream.readObject();
      } catch (ClassNotFoundException exception) {
          exception.printStackTrace();
          return false;
      }
      objectInputStream.close();
      fileInputStream.close();
  } catch (IOException exception) {
      exception.printStackTrace();
      return false;
  }
  </code></pre>
  <p>Save Player:</p>
  <pre><code>try {
      final FileOutputStream   fileOutputStream   = new FileOutputStream(file);
      final ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
      objectOutputStream.writeObject(user);
      objectOutputStream .close();
      fileOutputStream.close();
  } catch (IOException exception) {
      exception.printStackTrace();
      return false;
  }
  </code></pre>