I was wondering about how to serialize nested classes properly.
My problem is that there is one class that has a list of data that I need to serialize. The data though, is an object of many other classes. For example if Test1 is my data class, it has Test1a and others.
The error that I get if I don't write "implements Serializable" is that it is "java.io.NotSerializableException".
Should I just "implement Serializable" to all my classes then?
public class Test1 implements Serializable {
public void save() {
//some code to save
}
public void load() {
//some code to load
}
public class Test1a {
//something related
}
}
Yes. If you wan't to serialize a class, all the classes that it is dependent on should also be Serializable if you want to serialize them. If you don't wish to Serialize a member of a class, mark it as transient. When you mark something as transient, it will not be serialized. This means that when you deserialze an Object and it has a transient member, that transient member will get a default value.