What happens if your Serializable class contains a member which is not serializable? How do you fix it?

java, serialization

Solution

One of the fields of your class is an instance of a class that does not implement `Serializable`, which is a marker interface (no methods) that tells the JVM it may serialize it using the default serialization mechanism.

If all the fields of that class are themselves `Serializable`, easy - just add `implements Serializable` to the class declaration.

If not, either repeat the process for that field's class and so on down into your object graph.

If you hit a class that can't, or shouldn't, be made `Serializable`, add the `transient` keyword to the field declaration. That will tell the JVM to ignore that field when performing `Serialization`.

Problem

What happens if your Serializable class contains a member which is not serializable? How do you fix it?

Original source

Related problems