简体   繁体   中英

Java: Passing arguments at readObject/writeObject methods

I override the methods readObject/writeObject in order to customize the serialization of my objects. The problem is that these methods take no arguments. Is there anyway to pass parameters in these methods so I can use them in my serialization process?

Thank you in advance

No there is no option to pass parameters, moreover why would you need to do that.

Serialization, De serialization is the process of writing and reading an object state, so your class should have all the properties within it before being serialized or deserialized.

There shouldn't be any reason to pass parameters to these two methods.

You cannot change the signature of the read/write methods. One way of going around it is set and store your serialisation parameters in your objects and use them in the read/write methods.

Or you could make a static class, sth like SerializationParameter, and store your serialisation parameters there. Since they would be static you would not have to duplicate them in memory for each serialised object.

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