-2
public class TestBikes {
public static void main(String[] args){
Bicycle bike01, bike02, bike03;

bike01 = new Bicycle(20, 10, 1);
bike02 = new MountainBike(20, 10, 5, "Dual");
bike03 = new RoadBike(40, 20, 8, 23);

bike01.printDescription();
bike02.printDescription();
bike03.printDescription();
}
}

can line 3 (Bicycle bike01, bike02, bike03) and its 3 instance be rewritten as

Bicycle bike01 = new Bicycle(20, 10, 1);
Bicycle bike02 = new MountainBike(20, 10, 5, "Dual");
Bicycle bike03 = new RoadBike(40, 20, 8, 23);
Mat
  • 202,337
  • 40
  • 393
  • 406
Leonne
  • 85
  • 3
  • 10

2 Answers2

2

You seem to be asking whether you can initialize a variable as part of its declaration, e.g.:

Bicycle bike01 = new Bicycle(20, 10, 1);

rather than

Bicycle bike01;

bike01 = new Bicycle(20, 10, 1);

Yes, you can do that, and people commonly do. It doesn't change the resulting program.


Side note: When you find yourself writing variable names like bike01, bike02, etc., consider using an array or similar instead.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Yes of course , In the first case you just define them at the first then you initialize them but in the second case you define and initialize them simultaneously

Tareq Salah
  • 3,720
  • 4
  • 34
  • 48