The answer is:
- Define what you mean for an object reference to "exist".
- It is impossible to know how many object references were even created, without details of the
Point
and Circle
classes.
- The answer is irrelevant, because after the
main
method exits none of the objects will be reachable ... whether or not the references still "exist".
We might infer that at the point in time immediately before the main
method returns there will be one reachable reference to a Circle
object and one reachable reference to a Point
. But one has to make some (reasonable) assumptions about how those two classes are implemented to make that inference. (For example, one has to assume that the respective constructors don't add the Point
and Circle
reference to some static data structure.)
Are objects cleaned up when references to them are nulled?
No. Objects are cleaned up when the garbage collector runs, and it determines that the objects in question are no longer reachable. In this sense, "reachable" means that you can get to the object by following a chain of references to the object starting from:
- a static attribute of some class
- a local variable of some method that is currently being executed by some thread
- an attribute of some other reachable object, or
- an element of some other reachable array.
(I've simplified the explanations of GC and reachability a bit to avoid confusing the OP with things he/she won't understand yet.)