Why does the defaultWriteObject function have to be called first when writing into an ObjectOutputStream?

后端 未结 4 2075
清酒与你
清酒与你 2020-12-15 06:50

When I read the about the Serializable interface in Thinking in java, there is a sentence that says:

If you use the default

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 07:18

    I think the key word in the documentation is "should", which means that you don't have to.

    I think it's more of a best practice than anything else. If I read your code the first time and see that you defaulted the read/write on the first line, I can just say to myself "ok, done with 90% of the class", and concentrate on your custom code which takes care of all non-transient, non-static instance variables..

    The most important thing is to read/write in the same order. Apart from that you are free to do as you like.

提交回复
热议问题