小编Gre*_*reg的帖子

无法从静态上下文引用非静态变量

我写了这个测试代码:

class MyProgram
{
    int count = 0;
    public static void main(String[] args)
    {
        System.out.println(count);
    }
}
Run Code Online (Sandbox Code Playgroud)

但它给出了以下错误:

Main.java:6: error: non-static variable count cannot be referenced from a static context
        System.out.println(count);
                           ^
Run Code Online (Sandbox Code Playgroud)

如何让我的方法识别我的类变量?

java variables compiler-errors non-static

272
推荐指数
7
解决办法
61万
查看次数

标签 统计

compiler-errors ×1

java ×1

non-static ×1

variables ×1