Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head 1st Java - Counting Object References

 
Jim Shaw
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll have to look at the book again when I get home. Meanwhile, this other discussion on the same topic might help.
 
Jim Shaw
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc, I think I get why the 'b1.beeHA = ha' statement doesn't create new references to honeyPot: b1.beeHA is set to the other array, and it contains the exact same reference variable intances.

My question on 'r.k.kh' still stands.

Thanks again.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried the errata page?

http://www.oreilly.com/catalog/hfjava2/errata/

(i don't have the book)
 
Jim Shaw
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill.

Yes, I checked the errata page and didn't see any references to this exercise.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Shaw
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc for getting back to me.

You confirmed that it was a valid reference that wasn't counted.

I may post something to the errata website.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic