readObject() vs. readResolve() to restore transient fields
- by Joonas Pulakka
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?