1

This is my code for the linked list (not the main). The method "addLast" gives me the following error and i'm not sure how to resolve it: "non-static variable cannot be referenced from a static context". It is talking about this line: return new Node(x,null); I'd appreciate any help as to how to resolve this issue. Thank you

public class LinkedList
{
 private class Node 
 { 
 int item;
 Node link;
 public Node ()
 {
 item = Integer.MIN_VALUE;
 link = null;
 }
 public Node (int x, Node p)
 { 
 item = x;
 link = p;
 }
 } //End of node class
 private Node head;
 public LinkedList()
 {
 head = null;
 }
 //adds a node to the start of the list with the specified data
 //added node will be the first node in the list
 public void addToStart(int x)
 {
 head = new Node(x, head);
 }
 //adds a number at end of list 
 public static Node addLast(Node header, int x)
 { 
 // save the reference to the header so we can return it. 
 Node ret = header; 
 // check base case, header is null. 
 if (header == null) { 
 return new Node(x, null); 
 } 
 // loop until we find the end of the list 
 while ((header.link != null)) { 
 header = header.link; 
 } 
 // set the new node to the Object x, next will be null. 
 header.link = new Node(x, null); 
 return ret; 
 }
 //displays the list
 public void printList()
 {
 Node position = head;
 while(position != null)
 {
 System.out.print(position.item + " ");
 position = position.link;
 }
 System.out.println();
 }
}
asked Nov 29, 2014 at 21:31

3 Answers 3

1

Here are two solutions:

Make Node a static nested class:

private static class Node { ... }

Or, make the addLast method an instance method:

public Node addLast(Node header, int x) { ... }
answered Nov 29, 2014 at 21:33
3
  • It doesn't need to be a static nested class, though it may make sense for it to be so. Making it non-static is irrelevant to the OPs problem. Commented Nov 29, 2014 at 21:34
  • @ChrisDodd True, it's not absolutely necessary, but it seems more logical for Node to be independent of LinkedList. Commented Nov 29, 2014 at 21:39
  • Depends on what you want to do with the list. Making it non-static allows the Node to refer back to the head of the list it is contained in, which may be useful for some algortihms. Commented Nov 29, 2014 at 21:44
0

Remove the static qualifier from addLast -- it needs to be non-static to have a list to add to the end of. It also should not take (or return) a Node, as Node is a private nested class, so code outside this class doesn't know (or care) what a Node is, so can't have one to pass.

public void addLast(int x) {
 if (head == null) head = new Node(x, null);
 else {
 Node p = head;
 while (p.link != null) p = p.link;
 p.link = new Node(x, null); } }
answered Nov 29, 2014 at 21:36
6
  • I can't because addLast will have to be static in order for my main to access it. Commented Nov 29, 2014 at 21:40
  • No, your main just needs a LinkedList to call addLast on -- list = new LinkedList(); list.addLast(5); list.addLast(6);... Commented Nov 29, 2014 at 21:47
  • can you please explain the for loop? Commented Nov 29, 2014 at 21:49
  • It scans to the end of the list. Rewritten as a while to make it clearer Commented Nov 29, 2014 at 21:51
  • so when i type in your code, for the line head = new Node (x); it gives me the following error : no suitable constructor found for Node(int) Commented Nov 29, 2014 at 21:52
0

The answer is in the error line:

non-static variable cannot be referenced from a static context

Remove the static for the method addLast.

public Node addLast(Node header, int x) 
{
 ....
}
Machavity
31.8k27 gold badges97 silver badges106 bronze badges
answered Nov 29, 2014 at 21:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.