title: Restrict mutable object inside immutable object Java
body: <p>I am learning about immutable Objects.  I am trying this code</p>
  
  <pre><code>  public final class ImmutableObject {
  
      private final String name;
  
      private final NormalObject obj =  new NormalObject();
  
      public String getName() {
          return name;
      }
  
  
      public ImmutableObject(String name) {
          this.name = name;
          obj.setName(name);
      }
  
  
      public NormalObject getObj() {
  
          NormalObject tempObj = obj;
          return tempObj;
      }
  }
  
  public class NormalObject {
  
      private String name;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>I want to restrict the calling class from changing the value of name variable of NormalObject</p>
  
  <p>But the following code changes the value</p>
  
  <pre><code> ImmutableObject obj = new ImmutableObject("Siddle");
  
   System.out.println(obj.getObj().getName()); //prints Siddle
   obj.getObj().setName("Kelly");
  
   System.out.println(obj.getObj().getName()); //prints Kelly
  </code></pre>
  
  <p>How to restrict it?</p>