title: How can this class be used and assigned in this manner?
body: <p>I am currently trying to understand Singly linked lists.</p>
  
  <p>I don't understand some of the code in the SinglyLinkedList.java class. How can the Node class be called and then assigned like: private Node first;</p>
  
  <p>I would have thought that you would have to do something like this </p>
  
  <pre><code> Node&lt;T&gt; help =new Node&lt;&gt;();
  
   help = first;
  </code></pre>
  
  <p>If someone could explain, or provide me to a link that would help me, it would be much appreciated.
  Thanks!  </p>
  
  <pre><code>public class Node&lt;T&gt; {
  
      public T elem;
      public Node&lt;T&gt; next;
  
      public Node(T elem) {
          this.elem = elem;
          next = null;
      }
  
      public Node(T elem, Node&lt;T&gt; next) {
          this.elem = elem;
          this.next = next;
      }
  
      @Override
      public String toString() {
          return "Node{" + "elem=" + elem + '}';
      }
  
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>package list;
  
  /**
   *
   * @author dcarr
   */
  public class SinglyLinkedList&lt;T&gt; implements List&lt;T&gt; {
  
      private Node&lt;T&gt; first;
      private Node&lt;T&gt; last;
  
      public SinglyLinkedList() {
          first = null;
          last = null;
      }
  
      @Override
      public boolean isEmpty() {
          return first == null;
      }
  
      @Override
      public int size() {
          if (isEmpty()){
              return 0;
          } else {
              int size = 1;
              Node&lt;T&gt; current = first;
              while(current.next != null){
                  current = current.next;
                  size++;
              }
              return size;
          }
      }
  
      @Override
      public T first() {
          return first.elem;
      }
  
      @Override
      public void insert(T elem) {
          // if there is nothing in the list
          if (isEmpty()){
              first = new Node&lt;&gt;(elem);
              last = first;
          // if the list has elements already
          } else {
              // the new element will be the next of what was the last element
              last.next = new Node&lt;&gt;(elem);
              last = last.next; 
          }
      }
  
      @Override
      public void remove(T elem) {
          if (!isEmpty()){
              int index = 0;
              Node&lt;T&gt; current = first;
              while (current != null &amp;&amp; current.elem != elem){
                  current= current.next;
                  index++;
              }
              remove(index);
          }
      }
  
      @Override
      public String toString() {
          if (isEmpty()){
              return "Empty List";
          } else {
              String str = first.elem.toString() + " ";
              Node&lt;T&gt; current = first;
              while(current.next != null){
                  current = current.next;
                  str += current.elem.toString() + " ";
  
              }
              return str;
          }
      }
  
      @Override
      public void insertAt(int index, T e) {
  
          if (index == 0){
              first = new Node&lt;&gt;(e, first);
              if (last == null){
                  last = first;
              }
              return;
          }
          Node&lt;T&gt; pred = first;
          for (int i = 0; i &lt; index-1; i++) {
             pred = pred.next;
          }
          pred.next = new Node&lt;&gt;(e, pred.next);
          System.out.println(pred);
  
          if (pred.next.next == null){
              // what does this mean pred.next is?
              last = pred.next;
          }
      }
  
      @Override
      public void remove(int index) {
          if (index &lt; 0 || index &gt;= size()){
              throw new IndexOutOfBoundsException();
          } else if (isEmpty()){
              return;
          }
  
          if (index == 0){
              first = first.next;
              if (first == null){
                  last = null;
              }
              return;
          }
          Node&lt;T&gt; pred = first;
          for (int i = 1; i &lt;= index-1; i++) {
              pred = pred.next;
          }
          // remove pred.next
          pred.next = pred.next.next;
          if (pred.next == null){
              last = pred;
          }
      }
  
  }
  </code></pre>