Possible Duplicate:
non-static variable cannot be referenced from a static context (java)非静态变量c不能从静态上下文和非静态变量的引用不能从静态上下文
我的努力创建多个对象(在这种情况下,汽车)引用然后我试着检查c1(对象的名称)是否被创建(知道它是),然后检查是否创建了c2(同一类的其他对象)(知道它不是)。 我创建了班车: 包停车场;
/**
*
* @author HASLima
*/
public class Car {
String brand;
String plates;
int mileage;
public String getMarca() {
return brand;
}
public void setMarca(String brand) {
this.brand = brand;
}
public String getMatricula() {
return plates;
}
public void setMatricula(String plates) {
this.plates = plates;
}
public int getKilometros() {
return mileage;
}
public void setKilometros(int mileage) {
this.mileage = mileage;
}
}
然后创建的类公园: /* * 要改变这个模板,选择Tools |模板 *并在编辑器中打开模板。 */ 包停车场;
/**
*
* @author HASLima
*/
public class Park {
int nrOfCars;
int space;
Car[] c;
int a = 0;
public Park (int nrOfPlaces){
space = nrOfPlaces;
nrOfCars = 0;
}
public static void main(String[] args) {
Park park1 = new Park(5);
c[a] = new Car();
}
}
这里是问题,
C [A] =新汽车();
返回此错误:
非静态变量c不能从静态上下文 和 非静态变量不能从静态上下文
的原因是一样的,在其他1000这个网站上的问题提出了同样的问题。 –