Java 无法从**静态上下文**引用非静态方法。这里的静态内容是什么?

Java 无法从**静态上下文**引用非静态方法。这里的静态内容是什么?,java,static-methods,Java,Static Methods,我正在编写一个程序,从文本文件(只包含整数)中获取输入,将其放入链表并显示链表。这是我的密码: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; class Node{ int value; Node next; Node(){ next = null; } } public class ReverseLL{ pub

我正在编写一个程序,从文本文件(只包含整数)中获取输入,将其放入链表并显示链表。这是我的密码:

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”那样调用。我的问题是基于我的计划。我没有在其他类中创建该方法,为什么我需要更改为静态方法?所谓的静态内容是什么?感谢您向我解释。

您的主要方法是静态上下文,您正试图从中调用非静态方法display()。即使是在同一个班级也不行。要使display方法非静态,必须这样做

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

}

public void display(Node head){
    ...
}
简单地说,“静态”方法存在于类级别,而“非静态”方法存在于实例级别。在psvm方法中没有类的实例,可以从中调用非静态方法。所以这种方法根本不存在

您可以从这篇文章中阅读更多内容:
调用的静态上下文:

Class_Name.method_name();
调用的非静态上下文:

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

您不应该使用静态上下文调用非静态方法,反之亦然。

您正在从
公共静态void main(String[]args)
方法调用
display
方法

要解决必须使用静态方法的问题,您可以执行以下操作:

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...
        }       
    }
}

您还可以使用新的ReverseLL().dislpay(head);它将解决问题。

public static void main(String[]args)。@1615903我问的是另一个问题。@Jeffery这是一个完全重复的问题,并解释了为什么编译器不习惯在那里编译。@KevinEsche静态上下文是什么意思?我来自Javascript背景,对Java不熟悉。这是非常有洞察力的。非常感谢。