One object and one reference. The same 'honeyPot' reference is used in both arrays. It's just one, even though it appears 4 times in each array.
Actually, the reference is copied. There is still just one object, but the number of references increases. You can tell by assigning null to one of the array members; one reference is removed but the others still point to the Honey object.
I'd say there are 5 references: honeyPot and the 4 array members. b1.beeHa is not a reference to the Honey object but to the array of Honey references.
As for the "b1.beeHa = ha" statement, this does not copy the array but merely makes b1.beeHa reference the exact same array object. Keep in mind, arrays are a special type of objects. They are created on the heap as any other object, and can be references as any other object. They even have all the Object methods such as equals() and hashCode(), and even a public clone() method. The length field is basically nothing more than a public final int field.
The array itself is an object. That object contains references to all of the objects stored in the array.
When you assign that to another variable, all you do is create another reference to the same array object. This doesn't touch any of the internals of the array object. This is the semantics of assignment in Java.
If you really want to understand whats going on here, try a google search, using "shallow copy" and "deep copy" as keywords.
posted 10 years ago
Hello There ! First of all, thank you for all the replies.. Second, I must say I am confused. I will have to study further on the subject because I'm not getting how an array Object works. But, If it's not much of a Trouble, could someone please tell me Exactly How many references Do I have then?
I think that will help me understand what is actually happening in the heap And thank you very much.
Originally posted by Jose Campana: ...according to the HF book...
What page of Head First is this on? I would like to read some context.
Using the "reference" terminology here, I'm not so sure there's a straight-forward answer. As Head First says on page 54, a reference variable...
...holds something like a pointer. Or an address. Except, in Java we don't really know what is inside a reference variable. We do know that whatever it is, it represents one and only one object. And the JVM knows how to use the reference to get to the object.
"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
Hello Fellow Ranchers ! Marc, thanks for taking your time and giving me a detailed response... Now I get it. It's just that I was confused about the earlier concepts I read about Java and how it copies reference variables; but don't mind about it now, I believe I understand now how it copies the bits corresponding to the variable that references the array, and the copy itself can access the same array Object.
Thank you very much. I've already taken much of your time, so.. Once again thank you.
Will be posting again soon [I think] Sincerely, Jose
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?