Java 无法从静态上下文引用非静态变量

Java 无法从静态上下文引用非静态变量,java,variables,compiler-errors,non-static,Java,Variables,Compiler Errors,Non Static,我编写了以下测试代码: 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

我编写了以下测试代码:

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

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

要从静态方法访问它们,它们必须是静态成员变量,如下所示:

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

您必须理解类和该类的实例之间的区别。如果你在街上看到一辆车,即使你看不到它的型号,你也会马上知道它是一辆车。这是因为您将看到的内容与“汽车”类进行比较。该类包含与所有汽车相似的。把它看作一个模板或一个想法

同时,您看到的汽车是“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()。首先,静态变量不属于该类的任何特定实例。它们通过类的名称识别。静态方法不再属于任何特定实例。它们只能访问静态变量。假设您调用MyClass.myMethod(),myMethod是一个静态方法。如果在方法中使用非静态变量,它究竟如何知道使用哪些变量?这就是为什么只能从静态方法使用静态变量。我再次重申,它们不属于任何特定实例。

静态字段和方法连接到类本身,而不是其实例。如果您有一个类
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
    }
}
  • 首先要知道类的实例和类本身之间的区别。类对某些属性以及这些属性上下文中的整体行为进行建模。实例将为这些属性定义特定值

  • 任何绑定到static关键字的内容都可以在类的上下文中使用,而不是在类实例的上下文中使用

  • 作为上述情况的必然结果

  • 方法中的变量不能是静态的
  • 静态字段和方法必须使用类名调用,例如MyProgram7.main(…)
  • 静态字段/方法的生存期与应用程序的生存期相等

例如。 比如说,汽车具有属性颜色,并表现出“运动”行为。 这辆车的一个例子就是一辆时速25公里的红色大众甲壳虫

现在汽车的静态特性是道路上的车轮数(4),这将适用于所有汽车


我们先来分析一下你的程序。。 在程序中,第一个方法是
main()
,请记住它是静态方法。。。然后为该方法声明局部变量(compareCount、low、high等)。此变量的作用域仅为声明的方法,而不管它是静态方法还是非静态方法。所以你不能在这个方法之外使用这些变量。这是你犯的基本错误

然后我们来讨论下一点。你说过静电会杀了你。(它可能会杀了你,但它只会给你的程序带来生命!!)首先你必须了解基本的东西。 *静态方法只调用静态方法,并且只使用静态变量。 *静态变量或静态方法不依赖于该类的任何实例。(即,如果更改静态变量的任何状态,它将反映在类的所有对象中) *因此,可以将其称为类变量或类方法。 还有更多关于“静态”关键字的内容。 我希望你现在明白了。首先更改变量的范围并将其声明为静态(以便能够在静态方法中使用它)


给你的建议是:你误解了变量和静态功能的范围。对此要有清晰的认识。

最基本的是静态变量或静态方法在类级别。类级别的变量或方法在实例级别的方法或变量之前被加载。显然,没有加载的东西不能被使用。因此,java编译器不允许在运行时处理的问题在编译时解决。这就是为什么它会给你e
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();
  }
}
import java.io.*;

class HelloWorld {
    int myInt;      // this is a class variable that is unique to each object
    static int myInt2;  // this is a class variable shared by all objects of this class

    static void main (String [] args) {
        // this is the main entry point for this Java application
        System.out.println ("Hello, World\n");
        myInt2 = 14;    // able to access the static int
        HelloWorld myWorld = new HelloWorld();
        myWorld.myInt = 32;   // able to access non-static through an object
    }
}
class StaticTest {

      static int a;
      int b;
      int c;
}
class StaticTest {

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


      public static void main(String []cmd) {

             display();       
      }

}
class StaticTest {

      public static void main(String []cmd) {

             display();       
      }

}
NamCls.NamFnc();

System.out.println();
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);
        }
    }
}