views:

78

answers:

2

Hi,

I've got a class Foo, it's Serializable. It represents a graphical object, and I want it to restore its handles to the state before serializing. All data is stored within the object, I just need a method to be called in the right moment. How can I achieve it? Is this possible in Java?

(I have my Foos in a List in Bar object, and in some other places - that's why I don't want to do it manually.)

+9  A: 

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;

Implement this method, and call in.defaultReadObject(), and then do whatever custom logic you want.

Check the docs of java.io.Serializable for more details

Bozho
A: 

What handles? Are you aware that serialization already preserves object graphs?

EJP
I was talking about handles to a third-party library, written in C.
m01
OK well in that case the best place is in a readResolve() method, which is called after readObject(), which you therefore don't have to provide. Just do whatever you want to do and then return 'this'.
EJP