Possible Duplicate:
non-static variable cannot be referenced from a static context (java)
I am trying to create more than one objects (in this case cars) of the same class and then I am trying to check if c1 (the name of the object) was created (knowing that it was) and then check if c2 (other object of the same class) was created (knowing that it wasn't). I've created the class Car: package parkingLot;
/**
*
* @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;
}
}
And then created the class Park: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package parkingLot;
/**
*
* @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();
}
}
And here is the problem, the
c[a] = new Car();
Returns this error:
non-static variable c cannot be referenced from a static context and non-static variable a cannot be referenced from a static context