Java – cannot serialize a class that has already been serialized?

See the clip below

USPresident usPresident = new USPresident(56);
    try (ObjectOutputStream oos = new ObjectOutputStream(new
    FileOutputStream("/home/nikhil/dev/USPresident.data"))){
    oos.writeObject(usPresident);
    usPresident.setTerm(57);
    oos.writeObject(usPresident);
    System.out.println("Serialized");
    }

An example of a president was created for a term of 56.5 years Serialize it Reset the term to 57 and serialize it again

However, when I deserialize an object, its term is still 56 (not 57!)

I saw this explanation in the book,

However, as far as I know, the serialVersionUID is used to check whether the class object matches the serialized object during deserialization In addition, the serialVersionUID is used as the identifier of the object signature, not the state

I don't understand what's going on here Can anyone explain this behavior?

Take a look at the implementation of objectoutputstream, which is the implementation of writeobjet

public final void writeObject(Object obj) throws IOException {
    if (enableOverride) {
        writeObjectOverride(obj);
        return;
    }
    try {
        writeObject0(obj,false);
    } catch (IOException ex) {
        if (depth == 0) {
            writeFatalException(ex);
        }
        throw ex;
    }
}

See Boolean enableoverride This is required for writing 'modify' objects However, it can only be set to true when using subclasses of objectoutputstream See the protected constructor below,

protected ObjectOutputStream() throws IOException,SecurityException {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
    }
    bout = null;
    handles = null;
    subs = null;
    enableOverride = true;
    debugInfoStack = null;
}

Why such strange behavior?

Solution

Your book is wrong, while the staticversionuid is static and is used to handle different versions of the class itself

The idea of serialization is to take an active object (the object pointed to by USPresident) and save all its states in some external forms (bytes, XML, JSON) You can then restore the state to a copy of the object It's like saving a file in a word processor

However, if the object is modified after serialization, there is no connection You have written bytes to disk and they will not change because you call methods on objects When you recreate an object from these bytes, it will have the same value as it was saved If you forget to save your changes to a word processing document, the file on disk still has the old content

In your case, you encounter a quirk in Java serialization because you write the same object to the same objectoutputstream multiple times In order to serialize complex object relationships, Java serialization saves the object only once, and then links back to it when it is saved again Close and reopen the stream, and you should see the updated value

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>