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 {
...
为了能够从静态方法访问它们,它们需要是静态成员变量,如下所示:
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()
方法创建它所包含的类的实例(听起来很奇怪,但是因为main()
是用类而不是实例创建的,所以它可以这样做),然后调用实例方法(run()
)。您必须理解类和该类实例之间的区别。如果你在街上看到一辆车,即使你看不到它的型号,你也会马上知道它是一辆车。这是因为您将看到的内容与“汽车”类进行比较。该类包含与所有汽车相似的。把它看作一个模板或一个想法
同时,您看到的汽车是“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是一个静态方法。如果在方法中使用非静态变量,它究竟如何知道使用哪些变量?这就是为什么只能从静态方法使用静态变量。我再次重申,它们不属于任何特定实例。我将尝试向您解释静态的事情。首先,静态变量不属于该类的任何特定实例。它们通过类的名称识别。静态方法不再属于任何特定实例。它们只能访问静态变量。假设您调用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
}
}
静态字段和方法连接到类本身,而不是其内部
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);
}
}
}