Java – serialize a class variable that does not implement serialization
I have a class that implements serializable Class has another class object that does not implement serializability What should I do to serialize the members of this class
My class is like this
public class Employee implements Serializable{ private String name; private Address address; } public class Address{ private String street; private String area; private String city; }
Here, I didn't access the address class to make it serializable Please help. Thank you in advance
Solution
Of course, there is an obvious solution to put serializable on top I understand that this is not always an option
Maybe you can expand the address and put serializable on your child Then you create it so that employee has a child field instead of an address field
Here are some considerations:
>You can add employee The address field is left as the address type If you call employee Setaddress (New serializableaddress()), serializable > if address is null, the entire employee can be serialized even if the address type is not serializable. > If the address is marked transient, attempts to serialize the address are skipped This will solve your problem
Then there are other "serialization" frameworks, such as xStream, that do not require a markup interface to work It depends on whether your request is an option