Why are readObject and writeObject private, and why would I write transient variables explicitly?

前端 未结 6 1618
孤城傲影
孤城傲影 2020-12-02 12:47

I am reading the chapter on Serialization in Effective Java.

  1. Who calls the readObject() and writeObject()? Why are these methods declared private ?

6条回答
  •  广开言路
    2020-12-02 12:55

    Regarding the transient variable, best way to understand as if why do we declare transient variable and later on serialize them in writeobject method is to check/analyze/debug readobject/writeobject methods of LinkedList/HashMap/etc classes.

    This is generally done when you want to serialize/de-serialize the class variables in a pre-defined order and not rely on the default behavior/order.

提交回复
热议问题