the few things that i cant understand are inside the code..
The code should give out the car type, model type ,serial number. and the number of cars..
public class part5{
public static void main(String[] args) {
System.out.println(Car.getCounter());
Car[] cars =new Car[5];
for (int i = 0; i < cars.length; i++) {
cars[i]=new Car("Audi A"+(i+1), ""+(2000+i));
}
for (Car car : cars) {
System.out.println(car); // where does it go? and what does it print when it reaches there? whats the difference between , each for loop and a normal loop?
}
System.out.println(Car.getCounter());
cars=null;
System.out.println(Car.getCounter());// why to put this statement again after the variable cars was defined to be null?
}
}
class Car{
String name;
String model;
private static int counter;
int serialNumber;
public Car(String name, String model) { // why is the Car method has public before it? why the method has a capital letter ?
this.name = name;
this.model = model;
counter++;
serialNumber=counter; // why serial number doesnt have "this." before it?
}
public String toString() {
return "Car [name=" + name + ", model=" + model + ", serialNumber="
+ serialNumber + "]"; // when this is returned, where does that long string go to?
}
public static int getCounter(){
return counter;
}
}