This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm plodding through the "Head First Java - 2nd Ed." book, and just did the "Popular Objects" exercise (p. 267, ans. at bottom of p.270) at the end of Chapter 9.
The exercise directs the reader to examine some code that creates some objects and a great number of references to them, and to determine which single object ends up with the most references pointing to it.
The book claims that, just prior to the end of Main, there are a total of 12 references for a single honeyPot object. Judging from the diagram on p.270, it appears that the book isn't counting the b1.beeHA references, or the 'r.k.kh' reference.
My first question is, when b1.beeHA is set to ha, aren't we creating four new references (By the way, similar to when we set gc3 = gc2 in the previous, "Be the Garbage Collector" exercise, except those variables aren't arrays)?
My second question is, if 'r.k.kh' "refers to the object," why isn't that counted in the total?
It seems to me the correct answer is 17, not 12 object references to honeyPot
Originally posted by Jim Shaw: ... My question on 'r.k.kh' still stands...
Hmmm... Now that I'm looking at the book, I'm confused too.
I guess we're not seeing an arrow in the diagram because r.k.kh is not explicitly declared, so there is nowhere for the arrow to come from. But r.k.kh is a valid way to reference the object, so I would still expect this to count as an active reference.
Joined: Jan 25, 2008
Thanks Marc for getting back to me.
You confirmed that it was a valid reference that wasn't counted.