Cannot use "this" to reference an object to create a linked list
<p>I have a class (called AClass) containing an attribute of type AClass. I am trying to use this to create a linked list of integers. However, whenever I give a value to the data in AClass, it replaces the data in all the linked classes with this value. </p>

<pre><code>public class AClass {
    public AClass rest;
    public int data;

    public AClass (int tData) {
         data=tData;
         rest=null;
    }

    public void add(int item) {
        rest=this;          //This is what is causing the problem
        data=item;
    }
</code></pre>

<p>}</p>

<p>This is what I am using to test. My output should be 5,6,5 but I am getting 5,6,6.</p>

<pre><code>public class Test {
    public static void main(String[] args) {
        AClass aTest=new AClass(5);      //Creates a list with on element.
        System.out.println(aTest.data);  //Print that element for verification
        aTest.add(6); 
        System.out.println(aTest.data);      //Print the end element
        System.out.println(aTest.rest.data); //Print the next element, which should be 5
    }
}
</code></pre>

<p>I can't seem to figure out what I'm doing wrong. </p>
