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.