不能从静态上下文中引用非静态变量 [英] Non-static variable cannot be referenced from a static context

查看:31
本文介绍了不能从静态上下文中引用非静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这个测试代码:

class MyProgram{整数计数 = 0;public static void main(String[] args){System.out.println(count);}}

但它给出了以下错误:

Main.java:6: 错误:不能从静态上下文中引用非静态变量计数System.out.println(count);^

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

解决方案

您必须了解类和该类的实例之间的区别.如果您在街上看到一辆汽车,即使您看不到它的型号或类型,您也会立即知道这是一辆汽车.这是因为您将所看到的与汽车"进行了比较.该类包含类似于所有汽车.将其视为模板或想法.

同时,您看到的汽车是汽车"类的一个实例,因为它具有您期望的所有属性:有人驾驶它,它有引擎和车轮.

所以类说所有汽车都有颜色",实例说这辆车是红色的".

在面向对象的世界中,您定义了类,而在类内部,您定义了一个 Color 类型的字段.当类被实例化时(当你创建一个特定的实例时),内存是为颜色保留的,你可以给这个特定的实例一个颜色.由于这些属性是特定的,因此它们是非静态的.

静态字段和方法与所有实例共享.它们用于特定于类而不是特定实例的值.对于方法,这通常是全局辅助方法(如 Integer.parseInt()).对于字段,它通常是常量(例如汽车类型,即您的集合有限且不会经常更改的内容).

要解决您的问题,您需要实例化您的类的一个实例(创建一个对象),以便运行时可以为该实例保留内存(否则,不同的实例会相互覆盖您不想要的).

在你的情况下,试试这个代码作为起始块:

public static void main (String[] args){尝试{MyProgram7 obj = new MyProgram7();obj.run (args);}捕获(例外 e){e.printStackTrace();}}//这里的实例变量public void run (String[] args) 抛出异常{//把你的代码放在这里}

新的 main() 方法创建了它包含的类的一个实例(听起来很奇怪,但是因为 main() 是用类而不是实例创建的,它可以做到这一点)然后调用一个实例方法(run()).

I've written this test code:

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

But it gives the following error:

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

How do I get my methods to recognize my class variables?

解决方案

You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.

At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.

So the class says "all cars have a color" and the instance says "this specific car is red".

In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.

Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).

To solve your problem, you need to instantiate an instance (create an object) of your class so the runtime can reserve memory for the instance (otherwise, different instances would overwrite each other which you don't want).

In your case, try this code as a starting block:

public static void main (String[] args)
{
    try
    {
        MyProgram7 obj = new MyProgram7 ();
        obj.run (args);
    }
    catch (Exception e)
    {
        e.printStackTrace ();
    }
}

// instance variables here

public void run (String[] args) throws Exception
{
    // put your code here
}

The new main() method creates an instance of the class it contains (sounds strange but since main() is created with the class instead of with the instance, it can do this) and then calls an instance method (run()).

这篇关于不能从静态上下文中引用非静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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