title: immutable instance variable in java
body: <p>I tried to make the instance variable in the following class immutable by making it final..But it seems that I can modify its data.. Is only the reference immutable? (ie I cannot assign another instance of Y to that variable?)</p>
  
  <pre><code>public class StateModification {
      private final Y immutable ;
  
  
      public StateModification() {
          super();
          this.immutable = new Y(100);
      }
      public void setImmutableData(int x){
          this.immutable.setYnum(x);
      }
  
      public int getImmutableData(){
          return this.immutable.getYnum();
      }
  
  
      public static void main(String[] args) {
          StateModification sm = new StateModification();
  
          System.out.println("immutable="+sm.getImmutableData());
          sm.setImmutableData(222);
          System.out.println("immutable="+sm.getImmutableData());
      }
  }
  
  class Y{
      private int ynum;
      public Y(int ynum) {
          super();
          this.ynum = ynum;
      }
  
      public int getYnum() {
          return ynum;
      }
  
      public void setYnum(int ynum) {
          this.ynum = ynum;
      }
  }
  </code></pre>
  
  <p>o/p</p>
  
  <p>immutable=100<br>
  immutable=222</p>