简体   繁体   中英

serialPersistentFields in Java

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM