我写了这样的测试代码:

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

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


当前回答

您必须理解类和类的实例之间的区别。如果你在街上看到一辆车,即使你看不清车型,你也会立刻知道这是一辆车。这是因为您将所看到的内容与类“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())。

其他回答

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

为了能够从静态方法中访问它们,它们需要是静态成员变量,如下所示:

public class MyProgram7 {
  static Scanner scan = new Scanner(System.in);
  static int compareCount = 0;
  static int low = 0;
  static int high = 0;
  static int mid = 0;  
  static int key = 0;  
  static Scanner temp;  
  static int[]list;  
  static String menu, outputString;  
  static int option = 1;  
  static boolean found = false;

  public static void main (String[]args) throws IOException {
  ...

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

|*| 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);
        }
    }
}

您必须理解类和类的实例之间的区别。如果你在街上看到一辆车,即使你看不清车型,你也会立刻知道这是一辆车。这是因为您将所看到的内容与类“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())。

在这个程序中,你想使用count,所以将count方法声明为静态方法

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

您可以将此方法声明为公共私有并受保护。如果使用此方法,则可以创建安全应用程序。

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