I was going through this article to understand more about Java Serialization process. When it comes to uses of readObject/writeObject I could see two use cases:
- We can use
writeObjectto encrypt the byte code before it gets serialized. From the security point of view, that’s good thing. - we can use
readObjectto execute any specific piece of code that need to execute immediately after deserialization, and off course from poin#1, we can even usereadObjectto decrypt the byte code that was excrypted while serializing the object.
Is there any other practical scenario you’ve come across while serializing/deserializing objects by writing customr readObject/writeObject method? Or If you could point me to any place where I could see some decent and practical uses of readObject/writeObject?
Custom
readObjectmethods are also useful when you need to initialize transient (non-serialized) fields after the object has been deserialized.BTW, check out Effective Java, Chapter 11 (I’m not sure what the chapter/item number is in the 2nd ed.). It’s an excellent read on serialization.