无法对非静态方法进行静态引用? [英] Can not make static reference to non static method?

查看:244
本文介绍了无法对非静态方法进行静态引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,简而言之。我有两个课程。

So, in short. I have two classes.

package rpg;

public class Engine {
    public void main(String args[]) {
        Start.gameStart();
        System.out.println(menuResult);
    }
}

package rpg;
public class Start {
    int menuResult = 3;
    public int gameStart() 
    {
        return menuResult;
    }
    public int getMenuResult()
    {
        return Start.menuResult;
    }
}

它不断抛出错误'无法生成静态参考到非静态方法gameStart()'。
我确定我错过了一些简单的东西,却找不到它。
谢谢!

It keeps throwing up the error 'Cannot make static reference to non-static method gameStart()'. I'm sure I'm missing something simple, but can't find it. Thanks!

推荐答案

您需要创建的实例开始 class并在该实例上调用 gameStart()方法,因为 gameStart()是实例方法而不是静态方法。

You need to create instance of Start class and call gameStart() method on that instance because gameStart() is instance method not static method.

 public void main(String args[]) {
       new Start().gameStart();
       ..................
  }

只能使用类名作为perfix来访问静态方法。

Only static methods can be accessed by using class name as perfix.

这篇关于无法对非静态方法进行静态引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆