I have a class which implements Serializable. There is an other class object in the class which does not implement serializable. What should be done to serialize the member of the class.
My class is something 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 dont have access to the Address class to make it implement Serializable. Please help. Thanks in advance
The Transient variable is a variable whose value is not serialized during the serialization process. We will get a default value for this variable when we deserialize it.
You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.
The Student would not be Serializable, and it will act like a normal class. Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link.
Well of course there's the obvious solution to put Serializable
on it. I understand that's not always an option.
Perhaps you can extend the Address
and put Serializable
on the child you make. Then you make it so Employee
has a Child
field instead of an Address
field.
Here are some other things to consider:
Employee.address
field as an Address
type. You can serialize if you call the Employee.setAddress(new SerializableAddress())
Address
is null, you can serialize the whole employee even if Address
's type is not serializable.Address
as transient, it will skip trying to serialize Address
. This may solve your problem. Then there are other "serialization" frameworks like XStream that don't require the marker interface to work. It depends on your requirements whether that's an option though.
You caanot directly make this Address class serializable as you do not have access to modify it.
There are few options :
Please take a look at this stackoverflow link
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