readObject() vs. readResolve() to restore transient fields

Posted by Joonas Pulakka on Stack Overflow See other posts from Stack Overflow or by Joonas Pulakka
Published on 2010-05-10T08:11:40Z Indexed on 2010/05/10 8:14 UTC
Read the original article Hit count: 278

Filed under:
|
|

According to Serializable javadoc, readResolve() is intended for replacing an object read from the stream. But is it OK to use it for restoring transient fields, like so:

private Object readResolve() {
    transientField = something;
    return this;
}

as opposed to using readObject():

private void readObject(ObjectInputStream s) {
    s.defaultReadObject();
    transientField = something;
}

Is there any reason to choose one over other, when used to just restore transient fields?

© Stack Overflow or respective owner

Related posts about java

Related posts about serialization