创建对象时的“不能从静态上下文中引用此非静态变量”
2022-09-01 09:31:59
我编写了下面的代码来测试Java中类和对象的概念。
public class ShowBike {
private class Bicycle {
public int gear = 0;
public Bicycle(int v) {
gear = v;
}
}
public static void main() {
Bicycle bike = new Bicycle(5);
System.out.println(bike.gear);
}
}
为什么这会在编译过程中给我带来以下错误?
ShowBike.java:12: non-static variable this cannot be referenced from a static context
Bicycle bike = new Bicycle(5);
^