When I have Serializable
in a class, do I need to add Serializable
to all my objects within the class?
For example,
public class User implements Serializable{
private List<Role> role;
private Task task;
}
Do I need to add Serializable
to Role
and Task
, too?
public class Task implements Serializable{
// ...
}
public class Role implements Serializable{
// ...
}
Yes, you do; if your classes
Task
andRole
are notSerializable
, you'll get ajava.io.NotSerializableException
if you try to serialize an instance ofUser
.Ofcourse, if
Task
orRole
contain other non-primitive, non-transient fields, they would have to beSerializable
too, etc.