Note that there are some explanatory texts on larger screens.

plurals
  1. PODisable serialization cache
    text
    copied!<p>Is there a way to disable caching of serialized object in Java?</p> <p>I have this scenario:</p> <ol> <li>I have an object which is Serializable, I am serializing it, deserializing it, values are OK.</li> <li>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.</li> </ol> <p>Seems like the serializator is caching the values, or not?</p> <p>Thanks</p> <p>Copied this example from "fredrik" and adopted to my case:</p> <pre><code>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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload