title: Revert to old object state in Java
body: <p>I'm just learning Java but I keep running into the same problem over and over again</p>
  
  <p>How do i revert to an old state of some object efficiently?</p>
  
  <pre><code>public class Example {
      MyObject myLargeObject;
  
      public void someMethod(){
          MyObject myLargeMyObjectRecovery = myLargeObject;
  
          /**
           * Update and change myLargeObject
           */
  
          if(someCondition){
              //revert to previous state of myLargeObject
              myLargeObject = myLargeMyObjectRecovery;
          }
      }
  }
  </code></pre>
  
  <p>The above is how I would like the code to work but it obviously doesn't since myLargeObject and myLargeObjectRecovery are references to the same object.</p>
  
  <p>One solution is to create a copy constructor. This is fine for small objects but if I have a large object (in my project the object is a large 2D array meaning I would have to iterate over all of the entries), this way feels wrong.</p>
  
  <p>This must be a very common problem in Java, how do others get around it?</p>