When I add Serializable
to a class, do I need to add Serializable
to the classes of all fields too?
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
and Role
are not Serializable
, you'll get a java.io.NotSerializableException
if you try to serialize an instance of User
.
Ofcourse, if Task
or Role
contain other non-primitive, non-transient fields, they would have to be Serializable
too, etc.