How many objects exist just before this program stops running?
Two: the String object "A Greeting!" and
the HelloObject
.
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 main
method can find the HelloObject
through the reference in the variable anObject.
The main
method activates the speak()
method by anObject.speak().
The speak()
method can find the String
object
using the variable greeting
,
which is part of the HelloObject
object.
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.
(Obvious question:) Can there be several HelloObject
objects
created in the main()