go to previous page   go to home page   go to next page

Answer:

Yes.


One Variable, Three Objects

three holiday cards

Here is yet another testing program. This one constructs three cards one after the other. But the same reference variable card is used for each. So when the second card is constructed, the first one becomes garbage. And when the third card is constructed, the second one becomes garbage.

Garbage (remember) consists of objects that are no longer referred to.

The digram shows the situation just after the third card has been constructed. The garbage objects are shown ghosted.


public class CardTester
{
  public static void main ( String[] args )
  {
    Holiday card = new Holiday("Santa");
    card.greeting();
    
    card= new Holiday("Tinkerbell");
    card.greeting();
    
    card = new Holiday("Elvis");
    card.greeting();
  }
}

The program outputs (the same as the previous program):

Dear Santa,
Season's Greetings!

Dear Tinkerbell,
Season's Greetings!

Dear Elvis,
Season's Greetings!

QUESTION 9:

(Review: ) What happens to garbage?


go to previous page   go to home page   go to next page