I have the following program:
class Vehicle{
static int speed=50;
Vehicle(){
this.speed=speed;
}
}
class Bike4 extends Vehicle{
int speed=100;
void display(){
System.out.println(Vehicle.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike4 b=new Bike4();
b.display();
}
}
Supposing I don't want to use the keyword "super" and I simply call the function "Vehicle.speed" why do I have to change the type of the variable speed in the superclass to static? What would happen if I run the program without using the static keyword? (again, supposing it compiles)
staticisn't a type, it's a modifier...Vehicle.speedas you wrote it is not the speed of a given vehicle. It's the speed of all vehicles, which makes no sense.