Serialization of anonymous class in Java

前端 未结 4 1881
醉话见心
醉话见心 2021-01-12 12:59

Is it possible to searialize/desearialize anonymous class in Java?

Example:

ByteArrayOutputStream operationByteArrayStream = new ByteArrayOutputStrea         


        
相关标签:
4条回答
  • 2021-01-12 13:20

    This is certainly possible. Java generates an internal name for anonymous classes (Similar to DeclaredInThisClass$1, DeclaredInThisClass$2 if you declare them in a class called DeclaredInThisClass) and will happily serialize/deserialize them.

    0 讨论(0)
  • 2021-01-12 13:24

    Sure! In your case class Task should implement the Serializable interface.

    0 讨论(0)
  • 2021-01-12 13:41

    Note that in addition to Task implementing Serializable, the outer class must also be Serializable. You may end up serializing unnecessary member states.

    0 讨论(0)
  • 2021-01-12 13:43

    It is possible, by dangerous. The name/number of anonymous classes is generated by the compiler and is based on the order they appear in the file. e.g. if you swap the order of two classes, their names will swap as well. (Classes are deserialized by name)

    0 讨论(0)
提交回复
热议问题