Java – serialize object properties

When I use serializable in a class, do I need to add serializable to all objects in the class?

For example,

public class User implements Serializable{

private List<Role> role;
private Task task;
}

Do I also need to add serializable to roles and tasks?

public class Task implements Serializable{
    // ...
}

public class Role implements Serializable{
    // ...
}

Solution

Yes, you are; If your classes task and role are not serializable, if you try to serialize the user instance, you will get Java io. NotSerializableException.

Of course, if a task or role contains other non - primitive, non - transient fields, they must also be serializable, and so on

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>