File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC question, OCP book Exam 2 Question 1

 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe another angle:



In this case class Car "has-a" Engine.
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ted:your picture uploads are attractive
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic