aspose file tools*
The moose likes Beginning Java and the fly likes Head 1st Java - Counting Object References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head 1st Java - Counting Object References" Watch "Head 1st Java - Counting Object References" New topic
Author

Head 1st Java - Counting Object References

Jim Shaw
Greenhorn

Joined: Jan 25, 2008
Posts: 9
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

Joined: Aug 31, 2004
Posts: 11343

I'll have to look at the book again when I get home. Meanwhile, this other discussion on the same topic might help.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Shaw
Greenhorn

Joined: Jan 25, 2008
Posts: 9
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

Joined: Nov 08, 2007
Posts: 457
Have you tried the errata page?

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

(i don't have the book)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Jim Shaw
Greenhorn

Joined: Jan 25, 2008
Posts: 9
Thanks Bill.

Yes, I checked the errata page and didn't see any references to this exercise.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 25, 2008
Posts: 9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head 1st Java - Counting Object References
 
Similar Threads
Garbage Collection!
another GC question help
How many References does this Object have?
Garbage Collection Problem
Sun Cirtification