Java serialization: readObject() vs. readResolve()

前端 未结 10 973
花落未央
花落未央 2020-12-02 04:07

The book Effective Java and other sources provide a pretty good explanation on how and when to use the readObject() method when working with serializable Java class

10条回答
  •  难免孤独
    2020-12-02 04:45

    readResolve is used for replacing the object read from the stream. The only use I've ever seen for this is enforcing singletons; when an object is read, replace it with the singleton instance. This ensures that nobody can create another instance by serializing and deserializing the singleton.

提交回复
热议问题