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
TaskandRoleare notSerializable, you'll get ajava.io.NotSerializableExceptionif you try to serialize an instance ofUser.Ofcourse, if
TaskorRolecontain other non-primitive, non-transient fields, they would have to beSerializabletoo, etc.