How many objects exist just before this program stops running?

Answer:

Two: the String object "A Greeting!" and the HelloObject.

Picture of the Running Program

running program

Here is a picture showing the objects in the program just before it stops running. Of course, when it stops running all the objects become inactive and are reclaimed by the garbage collector.

While the program is running, there is a way to find every object:

  1. The main method can find the HelloObject through the reference in the variable anObject.

  2. The main method activates the speak() method by anObject.speak().

  3. The speak() method can find the String object using the variable greeting, which is part of the HelloObject object.

  4. System.out.println(greeting) in that method writes out the data in the String object.

Don't worry terribly much about all these details. Look them over a few times and then move on. Come back to this chapter in a few days when things have had a chance to soak in.


QUESTION 22:

(Obvious question:) Can there be several HelloObject objects created in the main() method?