Java 不能从静态上下文中引用非静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2694566/
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-08-13 10:55:00  来源:igfitidea点击:

non static method cannot be referenced from a static context

javastatic

提问by David

First some code:

首先是一些代码:

import java.util.*;
//...

class TicTacToe 
{
//...

public static void main (String[]arg) 
{ 

    Random Random = new Random() ; 
    toerunner () ; // this leads to a path of 
                   // methods that eventualy gets us to the rest of the code 
} 
//... 

public void CompTurn (int type, boolean debug) 
{ 
//...

        boolean done = true ; 
        int a = 0 ; 
        while (!done) 
        { 
            a = Random.nextInt(10) ;
            if (debug) { int i = 0 ; while (i<20) { System.out.print (a+", ") ; i++; }} 
            if (possibles[a]==1) done = true ; 
        } 
        this.board[a] = 2 ; 


}
//...

} //to close the class 

Here is the error message:

这是错误消息:

TicTacToe.java:85: non-static method nextInt(int) cannot be referenced from a static context
            a = Random.nextInt(10) ;
                      ^

What exactly went wrong? What does that error message "non static method cannot be referenced from a static context" mean?

究竟出了什么问题?错误消息“不能从静态上下文中引用非静态方法”是什么意思?

采纳答案by Anthony Forloney

You are calling nextIntstatically by using Random.nextInt.

nextInt通过使用静态调用Random.nextInt

Instead, create a variable, Random r = new Random();and then call r.nextInt(10).

相反,创建一个变量,Random r = new Random();然后调用r.nextInt(10).

It would be definitely worth while to check out:

绝对值得一试:

Update:

更新:

You really should replace this line,

你真的应该更换这条线,

Random Random = new Random(); 

with something like this,

像这样的东西,

Random r = new Random();

If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.

如果你使用变量名作为类名,你会遇到很多问题。同样作为 Java 约定,对变量使用小写名称。这可能有助于避免一些混乱。

回答by OscarRyz

You're trying to invoke an instance methodon the class it self.

您正在尝试在其自身的类上调用实例方法

You should do:

你应该做:

    Random rand = new Random();
    int a = 0 ; 
    while (!done) { 
        int a = rand.nextInt(10) ; 
    ....

Instead

反而

As I told you here stackoverflow.com/questions/2694470/whats-wrong...

正如我在这里告诉你的那样stackoverflow.com/questions/2694470/whats-wrong ...

回答by artgon

In Java, static methods belong to the class rather than the instance. This means that you cannot call other instance methods from static methods unless they are called in an instance that you have initialized in that method.

在 Java 中,静态方法属于类而不是实例。这意味着您不能从静态方法调用其他实例方法,除非它们是在您已在该方法中初始化的实例中调用的。

Here's something you might want to do:

以下是您可能想要执行的操作:

public class Foo
{
  public void fee()
  {
     //do stuff  
  }

  public static void main (String[]arg) 
  { 
     Foo foo = new Foo();
     foo.fee();
  } 
}

Notice that you are running an instance method from an instance that you've instantiated. You can't just call call a class instance method directly from a static method because there is no instance related to that static method.

请注意,您正在从已实例化的实例运行实例方法。您不能直接从静态方法调用类实例方法,因为没有与该静态方法相关的实例。

回答by erickson

Violating the Java naming conventions (variable names and method names start with lowercase, class names start with uppercase) is contributing to your confusion.

违反 Java 命名约定(变量名和方法名以小写开头,类名以大写开头)会导致您的混淆。

The variable Randomis only "in scope" inside the mainmethod. It's not accessible to any methods called by main. When you return from main, the variable disappears (it's part of the stack frame).

该变量Random仅在main方法内部“在范围内” 。调用的任何方法都无法访问它main。当您从 返回时main,变量消失(它是堆栈帧的一部分)。

If you want all of the methods of your class to use the same Randominstance, declare a member variable:

如果您希望类的所有方法都使用相同的Random实例,请声明一个成员变量:

class MyObj {
  private final Random random = new Random();
  public void compTurn() {
    while (true) {
      int a = random.nextInt(10);
      if (possibles[a] == 1) 
        break;
    }
  }
}