我写了这样的测试代码:

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

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


当前回答

静态字段和方法连接到类本身,而不是它的实例。如果你有一个类a,一个“正常”(通常称为实例)方法b和一个静态方法c,并且你为你的类a创建了一个实例a,那么对A.c()和a.b()的调用是有效的。方法c()不知道连接的是哪个实例,因此它不能使用非静态字段。

解决方案是要么使字段静态,要么使方法非静态。你的主要内容可以是这样的:

class Programm {

    public static void main(String[] args) {
        Programm programm = new Programm();
        programm.start();
    }

    public void start() {
        // can now access non-static fields
    }
}

其他回答

I will try to explain the static thing to you. First of all static variables do not belong to any particular instance of the class. They are recognized with the name of the class. Static methods again do not belong again to any particular instance. They can access only static variables. Imagine you call MyClass.myMethod() and myMethod is a static method. If you use non-static variables inside the method, how the hell on earth would it know which variables to use? That's why you can use from static methods only static variables. I repeat again they do NOT belong to any particular instance.

让我们先分析一下你的程序。 在你的程序中,你的第一个方法是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).

给你们的建议是:你们误解了变量作用域和静态函数的概念。对此要有清晰的认识。

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()。因此会发生编译时错误。

静态字段和方法连接到类本身,而不是它的实例。如果你有一个类a,一个“正常”(通常称为实例)方法b和一个静态方法c,并且你为你的类a创建了一个实例a,那么对A.c()和a.b()的调用是有效的。方法c()不知道连接的是哪个实例,因此它不能使用非静态字段。

解决方案是要么使字段静态,要么使方法非静态。你的主要内容可以是这样的:

class Programm {

    public static void main(String[] args) {
        Programm programm = new Programm();
        programm.start();
    }

    public void start() {
        // can now access non-static fields
    }
}

现在您可以在方法中添加/使用实例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();
  }
}