【问题标题】:non-static method cannot be referenced from a **static context**. What is static content here? [duplicate]不能从**静态上下文**引用非静态方法。这里的静态内容是什么? [复制]
【发布时间】:2016-11-04 07:31:10
【问题描述】:

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

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

}

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

【问题讨论】:

  • public static void main (String[] args).
  • @1615903 我问的是另一个。
  • @Jeffery 这是一个完整的副本,并解释了为什么编译器不会在那里编译。
  • @KevinEsche 静态上下文是什么意思?

标签: java static-methods


【解决方案1】:

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

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

}

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

【讨论】:

    【解决方案2】:

    调用的静态上下文:

    Class_Name.method_name();
    

    调用的非静态上下文:

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

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

    【讨论】:

    • 我来自 Javascript 背景,是 Java 新手。这是非常有见地的。谢谢!
    【解决方案3】:

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

    您可以从这篇文章中了解更多信息: What is the reason behind "non-static method cannot be referenced from a static context"?

    【讨论】:

      【解决方案4】:

      您正在从public static 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...
              }       
          }
      }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2023-03-31
          • 1970-01-01
          • 2017-02-06
          • 2014-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多