I have 2 Java classes car and camion (this is just an example). And I would like to initialise a variable "machine" depending on my function input:
void printInfo(boolean is_car){
if(is_car)
car machine = new car();
else
camion machine = new camion();
String info = machine.getInfo();
String type = machine.type();
...
}
It seems that I can't use the declared variables outside the if statement. Is there any solution for that? I would like to avoid putting lot of If statements in my function.