title: Escaping references - how to legitimately update object data?
body: <p>Been <a href="https://www.globalsoftwaresupport.com/memory-management-in-java" rel="nofollow noreferrer">reading</a> and <a href="https://www.lynda.com/Java-tutorials/What-escaping-references/802862/5011909-4.html" rel="nofollow noreferrer">watching</a> escaping references and I get the idea of why and how to fix it, but I'm still confused on one thing...</p>
  
  <p>What if the data does need to be updated? For example, I copied the code from the blog post down below and also added an additional class member of "email". Say the customer changed his email address. If this structure does not allow the object to be modified, how it the update done then?</p>
  
  <p>Interface:</p>
  
  <pre><code>public interface CustomerReadOnly {
      public String getName();
  }
  </code></pre>
  
  <p>Customer:</p>
  
  <pre><code>public class Customer implements CustomerReadOnly {
  
      private String name;
  
      private String email;
  
      public Customer(String name) {
          this.name = name;
      }
  
      @Override
      public String getName() {
          return this.name;
      }
  
      @Override
      public String toString() {
          return this.name;
      }
  }
  </code></pre>
  
  <p>Records</p>
  
  <pre><code>public class CustomerRecords {
  
      private Map&lt;String,Customer&gt; customerRecords;
  
      public CustomerRecords() {
          this.customerRecords = new HashMap&lt;&gt;();
      }
  
      public CustomerReadOnly getCustomerByName(String name) {
          return this.customerRecords.get(name);
      }
  }
  </code></pre>