-static variable big cannot be referenced from a static context
时间: 2023-10-28 19:55:00 浏览: 85
MIL-STD-113A_NOTICE-2.016129.PDF
This error occurs when you are trying to access a static variable from a non-static context or method. In other words, you are trying to access a variable that belongs to the class instead of an instance of the class.
To fix this error, you can either make the method or context static or create an instance of the class to access the variable.
For example, if you have a class with a static variable called "big" and a non-static method called "printBig", you cannot access "big" from "printBig" unless you make "printBig" static or create an instance of the class.
Here's an example of making "printBig" static:
public class MyClass {
static int big = 100;
public static void printBig() {
System.out.println(big);
}
}
And here's an example of creating an instance of the class:
public class MyClass {
int big = 100;
public void printBig() {
System.out.println(big);
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.printBig();
}
}
阅读全文