Posted by blaland
on May 21, 2008 at 9:27 PM PDT
I am currently working my way up the tutorials and learning Java from them and i have a small question.
I finished the section about classes and when i got to the exercises i came across this question:
Question 2: The following code creates one Point object and one Rectangle object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
Now, according to what i read in the tutorial the "point" object suppose to be eligible for garbage collection since it was assigned to "null" but the answer says:
Answer 2: There is one reference to the Point object and one to the Rectangle object. Neither object is eligible for garbage collection.
Now i'm confused.. and what's more confusing is that the next question (and answer) says this:
Question: How does a program destroy an object that it creates?
Answer: A program does not explicitly destroy objects. A program can set all references to an object to null so that it becomes eligible for garbage collection. But the program does not actually destroy objects.
which is exactly what i thought... does anyone else see the contradiction here?
I'd love if someone would explain it better for me..
Thanks in advance!