Can objects be passed by reference via serialization ?
1 posts in topic
Flat View  Flat View

Posted By:   Kiran_Bacche
Posted On:   Wednesday, June 8, 2005 01:12 AM

I serialize an object OBJ-1 and transfer the bytes over a socket. On the SAME JVM, a listener reads the bytes from a socket and deserializes to get an object OBJ-2.

Now normally OBJ-1 and OBJ-2 do not refer to the same object. Their references are different. So if I make any changes to OBJ-1 they are not reflected in OBJ-2.

Now is there a way I can make both OBJ-1 and OBJ-2 refer to the same object, i.e have same reference.

Since I ensure that serialization and deserialization happens on the SAME JVM, this should work I guess.


Re: Can objects be passed by reference via serialization ?

Posted By:   Jani_Kaarela  
Posted On:   Monday, June 13, 2005 07:46 AM

I can't think of an easy way to achieve this, since Serialization normally always creates a new instance, which naturally doesn't have the same identity/reference... Maybe a factory, which constructs the objects, and always compares to existing objects, returning the existing object if an identical one is found? You would need to customize the deserialization to use the factory. I think writing a readResolve() method would be sufficient. See Sun's Object Serialization doc.
About | Sitemap | Contact