I have started to read about serialization in Java and little bit in other languages too, but what if I have a generic class and I want to save a instance of it to file.
code example
public class Generic {
private T key;
public Generic() {
key = null;
}
public Generic(T key) {
this.key = key;
}
}
Whats the best way to save this kind of Object? (Of course there is more in my real ceneric class, but I'm just wondering the actual idea.)
解决方案
You need to make generic class Serializable as usual.
public class Generic implements Serializable {...}
If fields are declared using generic types, you may want to specify that they should implement Serializable.
public class Generic implements Serializable {...}
Please be aware of uncommon Java syntax here.
public class Generic implements Serializable {...}