aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question, OCP book Exam 2 Question 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC question, OCP book Exam 2 Question 1" Watch "GC question, OCP book Exam 2 Question 1" New topic
Author

GC question, OCP book Exam 2 Question 1

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1



In question 1 of OCP Exam 2 one of the sentences to gauge whether there is invalidity or validity states, "If Object X has a reference to Object Y, then Object Y cannot be GCed". I am always confused with garbage-collection because these can not be verified with System.out.println statements.

Does this mean that a reference can point to both ObjectX and Object Y at the same time? Why isnt , "If Object X has a reference to Object Y, then Object Y cannot be GCed" a true statement?

Respectfully,

Ted North - technical skills champion - people skills be damned
Diego Pettisani
Greenhorn

Joined: Jun 08, 2012
Posts: 3
Your graph is uncorrect.

The right design is this:

x --> ( OBJ x ) --> ( OBJ y )

This is a code example:



That's why "If Object X has a reference to Object Y, then Object Y cannot be GCed"
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Ted North wrote:Does this mean that a reference can point to both ObjectX and Object Y at the same time?

No. In general, when we say that an object "has" something, we mean that something is a member of the class. An instance of that class (object X) will thus keep alive a reference to the something (object y) as long as the object X instance is alive and kicking (unless/until that reference is made to point to a different object Y). Here, it means that object X is an instance of a class which has object Y as an instance field.

Why isnt , "If Object X has a reference to Object Y, then Object Y cannot be GCed" a true statement?

It could be the case that Object X is itself unreachable. In that case, and assuming there is no other way to reach object Y, then both objects can be garbage collected.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

Dennis Deems wrote:
Ted North wrote:Does this mean that a reference can point to both ObjectX and Object Y at the same time?

Here, it means that object X is an instance of a class which has object Y as an instance field.



----

Thanks Dennis Deems, I can understand this.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Maybe another angle:



In this case class Car "has-a" Engine.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

Bert Bates wrote:Maybe another angle:



In this case class Car "has-a" Engine.


So, when the class Car is nulled any reference to Engine will also be lost? Did I understand this correctly?



A comment from Bert Bates is the muffin on top of this java thread. Thank-you for taking the time to respond Bert.

Respectfully,

TN - javanaut
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ted North wrote:
So, when the class Car is nulled any reference to Engine will also be lost? Did I understand this correctly?

that is depends on relationship between Car and the Engine. Car has engine via dependence injection? if no, yes when car die, then engine die, else engine may be used by some one..

Engine:I give you engine, you use..no problem you die thats your business...

Engine: I am fully depends on this guy Car, if he destroyed, that is equal to I died....

got Ted?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

@Ted:your picture uploads are attractive
 
jQuery in Action, 2nd edition
 
subject: GC question, OCP book Exam 2 Question 1