我写了这样的测试代码:

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
    }
}

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

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

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

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

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 {
  ...