wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes References to an object in multiple collections. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "References to an object in multiple collections." Watch "References to an object in multiple collections." New topic
Author

References to an object in multiple collections.

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
I have a simple question about object references and Java collections.

Since objects are passed by reference and not value, can you pass a reference to the same object into multiple collections?

For Example: I have a collection named ScarryMonsters and a collection named HairyMonsters. Can both collections contain a reference to Bigfoot, and instance of the Monster class? If monster objects have an int variable name fearFactor, and it is modified by a method accessed through the ScarryMonsters collection, will new value be presented if accessed through the HairyMonsters collection?

Hope this example makes sense. Thanks for any help or suggestions.

Landon
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, two collections can contain references to the same object instance. There's no problem there at all. When you change the instance, you change the instance period, so whether you get a reference from one collection or from another you'll see the same results.

What do you think about garbage collection? What would have to happen before the a monster object can be gc'd?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
Thanks for the input Stan.

I'm guessing the reference to the object would have to be removed from both collections before it could be deleted.

Is this correct?

Thanks Again for the Response,

Landon
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
That is correct. In fact, "delete" means you as the programmer take an active measure to ensure the object is removed from memory. This is not possible in Java...but you can ensure that all references to a particular object are removed to allow the JVM to garbage collect that item.

This gets at the notion of "shallow copy" vs "deep copy". When dealing with data structures this comes up a lot. A shallow copy means you've copied the collection to the extent that you've created a new collection, but all of the references to contained objects are not unique--they refer to the same objects as are present in the other collection (a change to an object will show up in both collections since they point to that object). A deep copy means you copy everything all the way down. This is what would happen if you were to, for instance, serialize a LinkedList object that contained 10 items and send it over a socket connection to another JVM, where it is deserialized. Changes to one of the objects in the far away JVM will most certainly not show up in the local JVM, therefore that's a deep copy.

sev
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Sever oon,
Just randomly, that was a very good explanation of deep copy vs shallow copy. I shall have to keep that one in mind.
Jon
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, good take on the gc question. Now for fun look into weak references and weak collections. If the only references keeping something out of gc are "weak" then they kinda don't count and gc can take them. I haven't had reason to use this yet. Anybody else have real-world examples?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
SJ: Anybody else have real-world examples?

Theoretically, weak references are ideal for caches, but from my testing I found that my cache was flushed not just when the JVM was desparate for memory, but during the regular GC runs as well. Ever since, I am down on weak references.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: References to an object in multiple collections.