简体   繁体   中英

Questions regarding Externalizable and Serializable

We can take use of readResolve and writeReplace methods to designate replacement objects for both Externalizable and Serializable instances...

Just wanted to know couple of things:

1)Can readObject(ObjectInputStream is ) and writeObject(ObjectOutputStream os) methods be used in conjunction with readResolve() and writeReplace() for a Serilaizable class? Is it possible to replace the object and then perform modifications operations to the byte stream using readObject and writeObject 2)Exteralizable's writeExternal should explicitly coordinate with the supertype to save its state.Just wanted to know that is it true for Serializable super classes also(but not Externalizable) or the Serilizable classes state gets saved automatically when we write the instance to a stream?

Order of execution of methods if multiple special methods are present for a Serializable class

The methods are executed in the following order:-

When serialization occurs , the order of execution of methods is as follows :-

writeReplace
writeObject
readObject
readResolve
validateObject

Answer to Point 1)

Therefore,the answer to question 1 is yes

Answer to Point 2)

From JavaDoc Serializable interface Javadoc , for special methods such as

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException

The writeObject method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM