Hi friends, In the Java Sun site I read the following question.
Question : 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; ... The answer given for this in the Sun site is... Answer : There is one reference to the Point object and one to the Rectangle object. Neither object is eligible for garbage collection.
According to my knowledge only two references exists and the point object is eligible for garabage collection.
Unfortunately there is no more code shown on the sun-website... I think
a) As there is no (Point, int, int)-Constructor for Rectangle, this code won't compile (sure about this, just tried :-) )
b) If there was such a constructor you would need to know how the Point-class is built internally. If there were two instance-variables associated with a Rectangle-Object e.g.: One point and a Dimension, the given answer could be correct.
Joined: Mar 29, 2007
Strange, I wasn't able to edit my post...
Forget about b) even under the mentioned circumstances the point-Object should be eligible for GC
Point point = new Point(2,4); Rectangle rectangle = new Rectangle(point, 20, 20); point = null;
If we forget about what the standard Point and Rectangle constructors are and see the code only.
Copy of the Point reference is passed to the Rectangle constructor so that even if you set the Point reference to null, no object is eligible to GC. The reason is obvious, Point would still have active reference. But we can't be sure what the Rectangle is doing of Point inside. So we are not able to say it in loud pitch.
The point I am trying to make (and i am not sure of this point) is that we don't know what is happening to the Point reference inside the constructor.
if you use this:
and my hacked-together constructor, after you set point to null, i don't see how you have an active reference to the Point object created. I believe that the Point object is now eligible for GC.
But if the constructor DOES save the reference, then the Point object is NOT eligible.
So, i don't think you can answer this question with the code supplied
Joined: Feb 28, 2007
Actually Ravindranath didn't provide code inside the constructor. We only have supposition that in case the Rectangle constructor keeps the reference to the Point object. Outside the constructor null(ing) the point reference won't make the Point object eligible to Garbage collection.***
EXACTLY. We DON'T KNOW what's going on inside the 'real' constructor, so we have NO IDEA if the reference is saved or not.
Therefore, WE DON'T KNOW if the Point object is elligible or not. If it IS saved, the object is not elligible. If it is NOT saved, the object IS elligible.
You can make any suppositions you want, but really they're just guesses. [ April 11, 2007: Message edited by: Fred Rosenberger ]
Chandra, I'm sorry. i thought i was quoting your post, but i guess i hit the 'edit' button by mistake.
PLEASE accept my sincere apology. it was an accident, and i take full responsibility.
So, everyone, the above post by Chandra is really my reply to what he said. the quoted part is (i believe) what he originally said, and the messge is my reply. [ April 11, 2007: Message edited by: Fred Rosenberger ]
Joined: Feb 28, 2007
Ultimately we came to real conclusion: "GIVE US WHAT IS HAPPENING INSIDE THE Rectangle constructor" then only we can say whether Point is eligible for GC or not!
point=null makes point eligible for GC. As java passes by value call to Rectangle r=new Rectanle(point,2,3); Here point's reference's copy is sent.but point reference itself is of no use after this stmt.
Here point object reference is using in rectangle Object, so its not eligible for GC.
Joined: Mar 29, 2007
So we can say:
Practically: The Point object will be eligible for the gc as it is very unlikely (to say the least) that the constructor of the Point-class constructs an object with a still active reference to the assigned Point object
Theoretically: As we are not 100% sure about the implementation of classes in the Java-API, the question is not decidable.
In either case: The given answer on the sun-website is wrong.