*
The moose likes Beginning Java and the fly likes How many References does this Object have? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many References does this Object have?" Watch "How many References does this Object have?" New topic
Author

How many References does this Object have?

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Greetings Ranchers! I hope I can trouble someone with a little Query,
I have this in an exercise,


So, I have a lil' concept misunderstanding here because I would say that there are 9 active references to the same Honey Object. I believed that this line:

...Created 4 aditional references to the Honey Object, but according to the HF book, I am mistaken, and that line actually could only create one aditional reference to the Honey Object

Could someone Explain me why the line above creates just ONE reference?
And why doesn't it copy the 4 references contained inside the ha array?

Thank you very much in advance !
Jose
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

Honey honeyPot = new Honey();

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Originally posted by Abhinav Srivastava:

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
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.

- Adam
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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.
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349


How many refereneces do you see here 1 or 5?

To me it is just like
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hello,
Well, actually the problem isn't with this part of the code:

It's with this part of the code I posted Initially:


I want to know how many references to the Honey Object does b1.beeHA have?

Does it have one, Does it have four, or None? and WHY?

That is the Question. I'm sorry I'm being so annoying, but I don't get it.
Hope someone can help me.

Thank you very much.
Jose
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I found it. The problem is on page 267, and the solution is on page 270. This chapter is on object lifecycle, and we're talking about "active references" with respect to garbage collection.

I think the arrow diagram on page 270 is indicating that the line...

b1.beeHA = ha;

...is not creating any new references to the Honey object. Instead, it is only referencing (pointing to) the same array that "ha" is referencing -- as Rob and Adam explained above.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Originally posted by Jose Campana:
I want to know how many references to the Honey Object does b1.beeHA have?

Does it have one, Does it have four, or None? and WHY?

That is the Question. I'm sorry I'm being so annoying, but I don't get it.
Hope someone can help me.

b1.beeHa has 4 references, but they are the exact same 4 references as ha because of what marc just said. Therefore, these 4 references are not counted when counting the total number of references.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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
 
 
subject: How many References does this Object have?
 
Similar Threads
Eligible for garbage collection
Self Test Answers Chapter 3 Question 11
Head 1st Java - Counting Object References
Garbage Collection!
toString() returns String ?