overcoming a dealocating memory problem ?

From: Jole (no_spam_at_nospam.com)
Date: 05/12/04

Date: Wed, 12 May 2004 08:48:21 GMT

I'm using serialization to read in an object from a file. My code basically
does this:

1)MyClass obj = (MyClass) in.readObject();

('in' is an ObjectInputStream wrapped around a FileInputStream object, ie
the code reads an object stored in a file on disk)

Now, what happens is my code does this once at the start (works fine), but
later i need to do the same thing, to read in an updated version of the
object. ie, i need to do this again:

2) obj = (MyClass) in.readObject();

the compiler tells me that obj is constant and can't be assigned again. I
was hoping i could do something like this at step 2:

delete obj; //free memory
obj = (MyClass) in.readObject(); //read in new version of object from file
//and assign it to the same reference

(don't pay attention to the syntax, but the concepts..ie the above frees the
associated memory and then reads in a new copy from the file and assigns it
to the reference obj)

There doesn't seem to be a way to do it. Any ideas?