File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

References to an object in multiple collections.

 
Landon Blake
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Landon Blake
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic