-2

I am writing a program to get input from a text file (only contain integers), put it into linked list and display the linked list. Here is my code:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

class Node{
    int value;
    Node next;
    Node(){
        next = null;
    }
}


public class ReverseLL{
    public static void main(String[] args) throws FileNotFoundException{
        Scanner in = new Scanner(new File("input.txt"));
        Node head = null;
        Node tail = null;
        while(in.hasNextInt()){
            Node ptr = new Node();
            ptr.value = in.nextInt();
            if(head == null){
                head = ptr;
                tail = ptr;
            }else{
                tail.next = ptr;
            }
            tail = ptr;
        }
        display(head);
        in.close();
    }

    static void display(Node head){
        while(head!=null){
            System.out.print(head.value + " " + "\n");
            head = head.next;
        }
    }

}

It works now after I changed the display method to be static. However before I changed to static. The error said non-static method display(Node) cannot be referenced from a **static context I read some document about the static and no-static. To call a no-static, I need to instantiate an instance then call like instance.method. To call static method, you can call like "class.method". My question is based on my program. I did not create the method in other class, why I need to change to static method? What is the so called static content? Thank you for explaining it to me.

Jeffery
  • 134
  • 1
  • 2
  • 15

5 Answers5

4

Your main-method is the static context, and you are trying to call the non-static method display() from it. That doesn't work even if it is in the same class. To have the disply method non-static you have to do this.

public static void main(String[] args) throws FileNotFoundException{
    ReverseLL r = new ReverseLL();
    r.display(head);

}

public void display(Node head){
    ...
}
jawis
  • 56
  • 1
  • 4
4

static context of calling :

Class_Name.method_name();

non-static context of calling :

Class_Name object_name = new Class_Name();
object_name.method_name();

You should not call a non static method using a static context and vice versa.

Shree Naath
  • 477
  • 2
  • 5
  • 18
0

Simply said, "static" methods exist on class level, while "non-static" methods exist on instance level. You don´t have an instance of your class in your psvm-method, from where you could call the non-static method. So this method simply doesn´t exist.

You can read more from this post: What is the reason behind "non-static method cannot be referenced from a static context"?

Community
  • 1
  • 1
GoatyGuy
  • 133
  • 1
  • 8
0

You are calling your display method from the public static void main(String[] args) method.

To work around having to static methods you could do:

public class ReverseLL{

    public void run() throws FileNotFoundException{
        Scanner in = new Scanner(new File("input.txt"));
        Node head = null;
        Node tail = null;
        while(in.hasNextInt()){
            Node ptr = new Node();
            ptr.value = in.nextInt();
            if(head == null){
                head = ptr;
                tail = ptr;
            }else{
                tail.next = ptr;
            }
            tail = ptr;
        }
        display(head);
        in.close();
    }

    void display(Node head){
        while(head!=null){
            System.out.print(head.value + " " + "\n");
            head = head.next;
        }
    }

    public static void main(String[] args){
        ReverseLL reverseLL = new ReverseLL();
        try{
            reverseLL.run();
        }catch(FileNotFoundException e){
            // handle ex...
        }       
    }
}
Tadija Bagarić
  • 2,495
  • 2
  • 31
  • 48
0

you can also use the new ReverseLL().dislpay(head);it will root out the problem .

Yasir
  • 55
  • 3