Hi All,
May be I still don't get the sequence of how static methods are called and how class is loaded, but I stumbled across some strange behavior by static method. For your easy checking, I am copying the program here below. Can anyone tell me why the static method call to highID() always returns the number of objects that are instantiated?
class Vehicle {
int vID;
int speed;
int direction;
String owner;
private static int nextVID = 0;
Vehicle() {
vID = nextVID++;
}
Vehicle(String owner) {
this();
this.owner = owner;
}
static int highID() {
return nextVID;
}
};
public class VehicleDemo {
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
v1.owner = "Shane";
Vehicle v2 = new Vehicle("Darcy");
Vehicle v3 = new Vehicle("Tom");
System.out.println("Vehicle v1 details:");
System.out.print(v1.owner + " " + v1.direction + " " + v1.speed + " " + v1.vID);
System.out.print(" " + Vehicle.highID() + "\n");
System.out.println("Vehicle v2 details:");
System.out.print(v2.owner + " " + v2.direction + " " + v2.speed + " " + v2.vID);
System.out.print(" " + Vehicle.highID() + "\n");
System.out.println("Vehicle v3 details:");
System.out.print(v3.owner + " " + v3.direction + " " + v3.speed + " " + v3.vID);
System.out.print(" " + Vehicle.highID() + "\n");
}