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.
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
Joined: Dec 04, 2003
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.
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.
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
Joined: Jan 29, 2003
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?
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.
subject: References to an object in multiple collections.