title: Node losing the reference to another object when passed through function
body: <p>I'm working with double-ended queues for an assignment, and we're running into an issue where the object reference is disappearing from a node after being passed through an extremely simple method.</p>
  
  <p>Some important definitions:</p>
  
  <pre><code>class Node {
      String s;
      Node prev;
      Node next;
  
      ...
  
  }
  
  class Sentinel extends Node {
      Node prev;
      Node next;
     //Constructor uses that of Node
  }
  
  class Deque {
      Sentinel start;
  
      ...
  }
  </code></pre>
  
  <p>One method we are writing removes a Node from a deque, based on the given string.</p>
  
  <p>In deque:</p>
  
  <pre><code>public void removeSorted(String toRemove) {
      // System.out.println(this.start);
      // System.out.println(this.start.next);
      this.start.next.removeSorted(toRemove);
  }
  </code></pre>
  
  <p>The commented out println's show the correct Sentinel and Node.</p>
  
  <p>Then, in Node:</p>
  
  <pre><code>public void removeSorted(String toRemove) {
      if (this.s.equals(toRemove)) {
          // System.out.println(this.prev);
          // System.out.println(this.prev.next);
          this.prev.next = this.next;
          this.next.prev = this.prev;
      } else if (this.s.compareTo(toRemove) &gt; 0) {
          throw new RuntimeException("String does not exist in these nodes!");
      } else {
          this.next.removeSorted(toRemove);
      }
  }
  </code></pre>
  
  <p>The println for <code>this.prev</code> outputs the Sentinel on the first recursion, as expected. However, this.prev.next outputs null instead of the Node.</p>
  
  <p>This function only fails when trying to remove the first Node, directly after the Sentinel. If you try to remove any other Node, it works correctly, and trying to call <code>this.prev.next</code> results in a non-null answer.</p>
  
  <p>Why does the reference disappear when passing to the function (immediately after), since we've shown that the reference is there directly before calling the function?</p>