我写了这样的测试代码:

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);
                           ^

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


当前回答

ClassLoader负责加载类文件。让我们看看当我们编写自己的类时会发生什么。

示例1:

class StaticTest {

      static int a;
      int b;
      int c;
}

现在我们可以看到类“StaticTest”有3个字段。但实际上不存在b c成员变量。但是为什么??好的,让我看看。这里b c是实例变量。因为实例变量在对象创建时获得内存。所以这里b c还没有内存。这就是不存在b c的原因。所以只有a的存在。 对于ClassLoader,它只有一个关于a的信息。ClassLoader还不能识别b,c,因为它的对象还没有实例化。

让我们看另一个例子: 示例2:

class StaticTest {

      public void display() {
          System.out.println("Static Test");
      }


      public static void main(String []cmd) {

             display();       
      }

}

现在如果我们尝试编译这段代码,编译器会给出CE错误。 不能从静态上下文中引用非静态方法display()。

对于ClassLoader,它看起来是这样的:

class StaticTest {

      public static void main(String []cmd) {

             display();       
      }

}

在例2中,CE错误是因为我们从静态上下文调用非静态方法。因此ClassLoader不可能在编译时识别方法display()。因此会发生编译时错误。

其他回答

这是因为您不创建模型类的实例,您必须在每次使用非静态方法或变量时创建实例。

你可以很容易地修复这个,看看下面的图片

没有创建类的实例

我的模型类文件

通过创建实例,然后使用类的非静态方法或变量,很容易出错

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

Before you call an instance method or instance variable It needs a object(Instance). When instance variable is called from static method compiler doesn't know which is the object this variable belongs to. Because static methods doesn't have an object (Only one copy always). When you call an instance variable or instance methods from instance method it refer the this object. It means the variable belongs to whatever object created and each object have it's own copy of instance methods and variables.

静态变量被标记为静态,实例变量没有特定的关键字。

现在您可以在方法中添加/使用实例with

public class Myprogram7 {

  Scanner scan;
  int compareCount = 0;
  int low = 0;
  int high = 0;
  int mid = 0;  
  int key = 0;  
  Scanner temp;  
  int[]list;  
  String menu, outputString;  
  int option = 1;  
  boolean found = false;  

  private void readLine() {

  }

  private void findkey() {

  }

  private void printCount() {

  }
  public static void main(String[] args){

    Myprogram7 myprg=new Myprogram7();
    myprg.readLine();
    myprg.findkey();
    myprg.printCount();
  }
}

最基本的事情是静态变量或静态方法是在类级别。类级变量或方法在实例级方法或变量之前加载。显然,没有加载的东西是不能使用的。所以java编译器不让在运行时处理的事情在编译时解决。这就是为什么它会给你错误非静态的东西不能从静态上下文中引用。你只需要阅读类级作用域、实例级作用域和局部作用域。