While serializing a class in Java, you could take use of serialPersistentFields to override the default Java serialization behaviour.
Suppose, I want to serialize a static List, then I need to define it in an array of ObjectStreamField objects.
Can someone please help me to give an actual implementation of this whole scenario ?
There is an example using serialPersistentFields in the Java Object Serialization Specification: Section 1.5. The text says that the example code works for serialization and deserialization.
My reading of the code is that if one of the fields in your serialPersistentFields is a static field of the class, then it will be included in the serialization. However serialization of statics like this is dubious practice. The problem is understanding what happens when you deserialize a class with serialized statics. Will this clobber the existing static of the target class, or throw away the serialized static? Neither of these is a good option (in general).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With