我写了这样的测试代码:
class MyProgram
{
int count = 0;
public static void main(String[] args)
{
System.out.println(count);
}
}
但它给出了以下错误:
Main.java:6: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^
我如何让我的方法识别我的类变量?
The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.
Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class
As a corollary to the above
variables within a method can not be static
static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)
The lifetime of a static field/method is equivalent to the lifetime of your application
如。
例如,汽车具有属性颜色,并表现出行为“运动”。
以25公里每小时的速度行驶的红色大众甲壳虫为例。
现在,汽车的静态属性将是道路上的车轮数量(4),这将适用于所有汽车。
HTH
您必须理解类和类的实例之间的区别。如果你在街上看到一辆车,即使你看不清车型,你也会立刻知道这是一辆车。这是因为您将所看到的内容与类“car”进行比较。该类包含了类似于所有汽车的which。把它看作一个模板或一个想法。
同时,你所看到的汽车是类“car”的一个实例,因为它具有你所期望的所有属性:有人驾驶它,它有引擎,轮子。
因此,类说“所有汽车都有颜色”,实例说“这辆特定的汽车是红色的”。
在OO世界中,定义类,在类内部定义一个类型为Color的字段。当类被实例化时(当您创建一个特定的实例时),内存为颜色保留,您可以给这个特定的实例一个颜色。因为这些属性是特定的,所以它们是非静态的。
静态字段和方法与所有实例共享。它们用于特定于类的值,而不是特定的实例。对于方法,这通常是全局辅助方法(如Integer.parseInt())。对于字段,它通常是常量(如汽车类型,即你有一个有限的集合,不经常改变)。
为了解决这个问题,您需要实例化类的一个实例(创建一个对象),这样运行时就可以为该实例保留内存(否则,不同的实例就会相互覆盖,这是您不希望看到的)。
在你的情况下,试试下面的代码作为起点:
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
}
新的main()方法创建它所包含的类的实例(听起来很奇怪,但因为main()是与类一起创建的,而不是与实例一起创建的,所以它可以这样做),然后调用实例方法(run())。
让我们先分析一下你的程序。
在你的程序中,你的第一个方法是main(),记住它是静态方法…然后声明该方法的局部变量(comparate、low、high等)。这个变量的作用域只是声明的方法,不管它是静态方法还是非静态方法。你不能在这个方法之外使用这些变量。这是你犯的基本错误。
Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing.
*Static method calls only the static method and use only the static variable.
*Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class)
*Because of this you call it as a class variable or a class method.
And a lot more is there about the "static" keyword.
I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).
给你们的建议是:你们误解了变量作用域和静态函数的概念。对此要有清晰的认识。
The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.
Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class
As a corollary to the above
variables within a method can not be static
static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)
The lifetime of a static field/method is equivalent to the lifetime of your application
如。
例如,汽车具有属性颜色,并表现出行为“运动”。
以25公里每小时的速度行驶的红色大众甲壳虫为例。
现在,汽车的静态属性将是道路上的车轮数量(4),这将适用于所有汽车。
HTH