A single reference variable car
is used in turn for each object.
The output to the screen of this program is the same as the previous.
However, since the reference variable car
is used when
the second Car
is constructed, the first car becomes garbage before
the end of the program.
Car car = new Car( 32456, 32810, 10.6 ); System.out.println( "Miles per gallon of car 1 is " + car.calculateMPG() ); car = new Car( 100000, 100300, 10.6 ); System.out.println( "Miles per gallon of car 2 is " + car.calculateMPG() );
The picture shows the situation just after the second car is constructed. The first car no longer has a reference to it, so it is now garbage.