non-static method cannot be referenced from a static context
Why one add method GList.add("Luke")caused this error but the other add q1.add("a") is ok? Thanks a lot. Here is the code, which has 3 class.
import java.util.*;
import java.awt.*;
public class AList {
public static void main(String[] args)
{
GList g = new GList();
GList.add("Luke");
//System.out.println(n2.next.data);
AList ba = new AList();
ArrayList<String> q1= new ArrayList<String>();
for(int i=0;i<5;i++)
q1.add("a");
}
}
public class GList<E>
{
private static class Node<E>
{
public E data;
public Node<E> next;
public Node(E data, Node<E> next)
{
this.data = data;
this.next = next;
}
}
private Node<E> head;
public GList()
{
head = new Node<E>(null, null);
}
public void add(E e)
{
Node<E> c=head;
while ( c.next!=null)
c=c.next;
Node<E> newNode=new Node<E>(e, null);
c.next=newNode;
}
}