Note that there are some explanatory texts on larger screens.

plurals
  1. POSingly linked list - Cannot make a static reference to the non-static type
    primarykey
    data
    text
    <p>I am working a singly linked list to learn Java.</p> <p>I have a working linked list but it binds to integer type, now I am trying to implement a generic typed linked list by changing all the integer declaration to generic type E.</p> <p>The compiler keeps complaining that it "Cannot make a static reference to the non-static type E". The code still runs. Does anybody know how to fix this error " ?</p> <p>I think it has something to do with whether the generic type E is static like integer or double or if it is a reference type like String or other classes.</p> <pre><code>public class TestingLinkedList&lt;E&gt; { private E value; private TestingLinkedList&lt;E&gt; next; /* * Default Constructor * * @param value an absolute E value for the current Node * * @param next an absolute RecursiveLinkedList value for the current Node */ public TestingLinkedList(E value, TestingLinkedList next) { this.value = value; this.next = next; } /* * Constructor Empty, when user supplies an empty for the constructor uses * value = - 1 and next = null as input parameters * * @param value an absolute int value for the current Node * @param next an absolute RecursiveLinkedList value for the current Node */ public static final TestingLinkedList EMPTY = new TestingLinkedList(null,null) { public TestingLinkedList remove(E n) { return this; }; public String toString() { return ""; }; }; /* * if the current node is null return false * else if current value is the chosen value * then return true. Otherwise call the contains * method of the next item in queue * * @param value an absolute int value for the current Node * @param RecursiveLinkedList object of the remove item * */ public TestingLinkedList remove(E n) { if (value == n) { return next; } // Call the remove method of the next Node if the selected Node is not // the current node then construct and return the next method return new TestingLinkedList(value, next.remove(n)); } /* * if the current node is null return false * else if current value is the chosen value * then return true. Otherwise call the contains * method of the next item in queue * * @param value an absolute int value for the current Node * @param boolean * */ public boolean contains(E n) { if (next == null) { return false; }else if (value == n) { return true; } else { return next.contains(n); } } public String toString() { return value + "," + next.toString(); } /* * * Testing Methods for RecursiveLinkedList * * */ public static void main(String[] args) { TestingLinkedList l = new TestingLinkedList(1, new TestingLinkedList(2, new TestingLinkedList(2, new TestingLinkedList(3, new TestingLinkedList(4, EMPTY))))); System.out.println(" Test to String Method : " + l.toString()); System.out.println(" Test remove method " + l.remove(1).toString()); System.out.println(" Test contains method " + String.valueOf(l.contains(4))); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload