A single reference variable cone
is used in turn for each object.
The output to the screen of this program is the same as the previous.
However, when a reference to
the second Cone
object is assigned to cone
,
the first Cone
object becomes garbage.
Cone cone = new Cone( 1.2, 4.56 ); System.out.println( "cone area: " + cone.area() + " volume: " + cone.volume() ); cone = new Cone( 3.0, 1.2 ); System.out.println( "cone area: " + cone.area() + " volume: " + cone.volume() );
The picture shows the situation just after the second Cone
is constructed.
The first Cone
no longer has a reference to it, so it is now garbage.
(Review Questions:)