我正在写一个程序从文本文件中获取输入(只包含整数),将其放入链接列表并显示链接列表。这里是我的代码:非静态方法不能从**静态上下文**中引用。什么是静态内容?
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;
}
}
}
它现在以后,我改变了显示器的方法是静态。然而在我改变为静态之前。该错误说非静态方法显示(节点)不能从静态上下文引用我读了一些关于静态和无静态的文档。要调用一个非静态的,我需要实例化一个实例,然后调用像instance.method。要调用静态方法,你可以调用像“class.method”。我的问题是基于我的程序。我没有在其他类中创建该方法,为什么我需要更改为静态方法?什么是所谓的静态内容?谢谢你向我解释。
public ** static ** void main(String [] args)。 – DimaSan
@ 1615903我在问另外一个。 – Jeffery
@Jeffery它是一个完整的重复,并解释了为什么编译器不会在那里编译。 – SomeJavaGuy