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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40417422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:12:06  来源:igfitidea点击:

non-static method cannot be referenced from a **static context**. What is static content here?

javastatic-methods

提问by Jeffery

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

在我将显示方法更改为static后,它现在可以工作了。但是在我改成静态之前。错误说不能从 **static 上下文中引用非静态方法 display(Node)我阅读了一些关于静态和非静态的文档。要调用非静态,我需要实例化一个实例,然后像 instance.method 一样调用。要调用静态方法,您可以像“class.method”一样调用。我的问题是基于我的程序。我没有在其他类中创建方法,为什么要改为静态方法?什么是所谓的静态内容?谢谢你给我解释。

回答by Jon Are Wisting

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.

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

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

}

public void display(Node head){
    ...
}

回答by Shree Naath

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.

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

回答by Tadija Bagari?

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

您正在displaypublic static void main(String[] args)方法中调用您的方法。

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

回答by Yasir

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

你也可以使用新的 ReverseLL().dislpay(head);它会根除问题。

回答by GoatyGuy

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.

简单地说,“静态”方法存在于类级别,而“非静态”方法存在于实例级别。您的 psvm-method 中没有您的类的实例,您可以从中调用非静态方法。所以这种方法根本不存在。

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

您可以从这篇文章中阅读更多内容: “不能从静态上下文中引用非静态方法”背后的原因是什么?