我写了这样的测试代码:

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

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


当前回答

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

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

其他回答

对于所有初学者来说,解释静态关键词有点困难。 当你更多地使用类和对象时,你会清楚地了解它。

|*| Static:静态项可以用类名调用 如果你在代码中观察,有些函数是直接用类名调用的,比如

NamCls.NamFnc();

System.out.println();

这是因为NamFnc和println将在它们之前使用关键字static来声明。

|*|非静态:非静态项目可以用类变量调用 如果它不是静态的,你需要类的一个变量, 在类变量和后面加上点 然后调用function。

NamCls NamObjVar = new NamCls();
NamObjVar.NamFnc();

下面的代码简洁地解释了你

类中的静态和非静态函数:

public class NamCls
{
    public static void main(String[] args)
    {
        PlsPrnFnc("Tst Txt");

        NamCls NamObjVar = new NamCls();
        NamObjVar.PrnFnc("Tst Txt");
    }

    static void PlsPrnFnc(String SrgPsgVal)
    {
        System.out.println(SrgPsgVal);
    }

    void PrnFnc(String SrgPsgVal)
    {
        System.out.println(SrgPsgVal);
    }
}

类中的静态类和非静态类:

public class NamCls
{
    public static void main(String[] args)
    {
        NamTicCls NamTicVaj = new NamTicCls();
        NamTicVaj.PrnFnc("Tst Txt");

        NamCls NamObjVar = new NamCls();
        NamNicCls NamNicVar = NamObjVar.new NamNicCls();
        NamNicVar.PrnFnc("Tst Txt");
    }

    static class NamTicCls
    {
        void PrnFnc(String SrgPsgVal)
        {
            System.out.println(SrgPsgVal);
        }
    }

    class NamNicCls
    {
        void PrnFnc(String SrgPsgVal)
        {
            System.out.println(SrgPsgVal);
        }
    }
}

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

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.

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

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

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

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

没有创建类的实例

我的模型类文件

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