Why does writeObject throw java.io.NotSerializableException and how do I fix it?

前端 未结 3 792
自闭症患者
自闭症患者 2020-11-22 12:22

I have this exception and I don\'t understand why it would be thrown or, how I should handle it.

try {
    os.writeObject(element);
} catch (IOException e)          


        
3条回答
  •  被撕碎了的回忆
    2020-11-22 12:45

    java.io.NotSerializableException can occur when you serialize an inner class instance because:

    serializing such an inner class instance will result in serialization of its associated outer class instance as well

    Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged

    Ref: The Serializable Interface

提交回复
热议问题