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
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:
The
mainmethod can find theHelloObjectthrough the reference in the variableanObject.The
mainmethod activates thespeak()method byanObject.speak().The
speak()method can find theStringobject using the variablegreeting, which is part of theHelloObjectobject.System.out.println(greeting)in that method writes out the data in theStringobject.
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 21:
(Obvious question:) Can there be several HelloObject objects
created in the main()