When I read the about the Serializable
interface in Thinking in java
, there is a sentence that says:
If you use the default mechanism to write the non-transient parts of your object, you must call defaultWriteObject( ) as the first operation in writeObject( ), and defaultReadObject( ) as the first operation in readObject( ).
And in docs.oracle.com 5.6.2:
Adding writeObject/readObject methods - If the version reading the stream has these methods then readObject is expected, as usual, to read the required data written to the stream by the default serialization. It should call defaultReadObject first before reading any optional data. The writeObject method is expected as usual to call defaultWriteObject to write the required data and then may write optional data.
So If I don't call defaultWriteObject
first, and if I write something else before that call, will there be any problem? I have tried it, but it seems that it still works well in my example. So If there is to be any problem, under what condition could it occur ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…