How to serialize a generic class in Java?

前端 未结 2 1096
孤街浪徒
孤街浪徒 2020-12-31 10:57

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.

2条回答
  •  离开以前
    2020-12-31 11:21

    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 {...}

提交回复
热议问题