英文:
Not serializable exception even though all fields are serializable
问题
我有以下类定义,我想将其保存在文件中,它的所有字段都是可序列化的,那么为什么这个类不是可序列化的?
我在其他回答中看到不需要使用getter和setter,所以我已经在类定义中省略了它们。
private class MyClass implements Serializable
{
private static final long serialVersionUID = 314L;
public HashMap<Long, String> allTags = new HashMap<Long, String>();
public HashMap<String, String> notes = new HashMap<String, String>();
public HashMap<String, ArrayList<Long>> tags =
new HashMap<String, ArrayList<Long>>();
}
英文:
I have the following class definition which I want to save in a file, and all its fields are serializable, so why isn't the class serializable?
I've read in other responses that I don't need getters and setters, so I've omitted them from the class definition.
private class MyClass implements Serializable
{
private static final long serialVersionUID = 314L;
public HashMap<Long, String> allTags = new HashMap<Long, String>();
public HashMap<String, String> notes = new HashMap<String, String>();
public HashMap<String, ArrayList<Long>> tags =
new HashMap<String, ArrayList<Long>>();
}
答案1
得分: 2
直截了当地,封装的类需要是Serializable
。
英文:
Straight forward, the enclosing class needs to be Serializable
.
答案2
得分: 0
如果这是一个内部类,请使外部类可序列化,并序列化外部类实例。
英文:
If this is an inner class, make the outer class serializable and serialzie outer class instance
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论