title: Changing references to Objects java
body: <p>Code from CCI book:</p>
  
  <pre><code>public class ListNode {
  
    int val;
    ListNode next = null;
  
    ListNode(int x) {
      this.val = x;
    }
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>  public void appendToTail(int d) {
      ListNode end = new ListNode(d);
      ListNode n = this;
      while (n.next != null) {
        n = n.next;
      }
      n.next = end;
    }
  </code></pre>
  
  <p>Original: 5 -> 10 -> 15</p>
  
  <p>As I understand, in this case "n" and "this" refer to same object yet, after debugging and stepping through:</p>
  
  <p>"n" becomes 15 -> 20</p>
  
  <p><strong>"this" becomes 5 -> 10 -> 15 -> 20</strong></p>
  
  <p>How can this be? When I change "n", "this" should change as well?</p>
  
  <p>So everything I did on "n" ,BESIDES appending 20, did not reflect on "this".</p>
  
  <p>UPDATE:</p>
  
  <p>Anyone who has same question should read <a href="https://stackoverflow.com/a/73021/6817610">this</a>.</p>