Is there a way to disable caching of serialized object in Java?
I have this scenario:
- I have an object which is Serializable, I am serializing it, deserializing it, values are OK.
- On the same object, I am changing some value, I am serializing it, deserializing it, values are NOT OK, values are same as the first initially loaded.
Seems like the serializator is caching the values, or not?
Thanks
Copied this example from "fredrik" and adopted to my case:
public class SerialDeserial {
public static void main(String[] args) {
try {
ChangingObject obj = new ChangingObject();
obj.foo=1;
// Write it
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.foo"));
os.writeObject(obj);
os.flush();os.close();
// Read the object
ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.foo"));
ChangingObject objDummy = (ChangingObject)is.readObject();
System.out.println("objDummy.foo is "+objDummy.foo);
// Change it
obj.foo=2;
// Write it
os = new ObjectOutputStream(new FileOutputStream("test.foo"));
os.writeObject(obj);
os.flush();os.close();
// Read the object
is = new ObjectInputStream(new FileInputStream("test.foo"));
objDummy = (ChangingObject)is.readObject();
System.out.println("objDummy.foo is "+objDummy.foo); // this returns "1" insted of "2"
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ChangingObject implements Serializable {
public int foo;
}