Results 1 to 5 of 5
  1. #1
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default 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;
    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    q1.add is okay because it is referenced from a static context

    Java Code:
    public [B][U][COLOR="Red"]static[/COLOR][/U][/B] void main(String[] args)

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    q1.add is okay because it is referenced from a static context

    Java Code:
    public [B][U][COLOR="Red"]static[/COLOR][/U][/B] void main(String[] args)
    That is bull (sorry for saying so). The add( ... ) method can only be called on an object. q1 is such an object so you can say q1.add( ... ). You can't just say GList.add( ... ) because GList isn't an object you can say add( ... ) to. People who studied SmallTalk never had this problem ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    My bad for the guess work.

  5. #5
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 03-14-2011, 12:05 AM
  2. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  3. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  4. Replies: 11
    Last Post: 11-19-2008, 03:13 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •