Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Object Reference

The problem I met is with Java I try to use serialization or clone ,whatever, to backup an object(name it "object1"), so later if any dirty data occurs , I could take the stored object back and rollback.
But unfortunately the object I took back(using deserialization) is actually generating a new object (name it "object2"). The reference of object1 is not equal to object2 which means they are different objects in the memory space. But I hope I can restore the backup object to the original memory space. Is there any good method to do so? Or is it possible? Thx
  • 2
1 Solution
FileInputStream istream = new FileInputStream("file.tmp");
ObjectInputStream p = new ObjectInputStream(istream);

obj1 = (ObjectType1)p.readObject();
obj2 = (ObjectType2)p.readObject();


Assume reference name in VM for objects are obj1, obj2 . Now obj1,obj2 will point to object retreived from the persistance store.
First at all deserialization is reading a class information and instance object information. After reading that information VM creates new object instance and initializes it with the serialized object information. This a new object with its own reference. There is no way to restore the object in its original memory address, because that is in the contrary with the Java concept. Java is not dealing with pointers and there is a constantlly running garbage collector, so the valid reference in one moment can an invalid reference in the future.
magichAuthor Commented:
hi msteriev:
So based on you comment. before serialization, if object1 is referred by some other objects. afte serialization and deserialization such refering relation will lose. Since serialization can keep the referance from object1 to other objects but not the referance from other objects to object1. Is it? thx

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now