java.io.InvalidClassException: no valid constructor

后端 未结 3 726
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 12:50

When I run below program, I am getting exception as

java.io.InvalidClassException: Files.SerializationMain; Files.SerializationMain; no valid constructor
           


        
相关标签:
3条回答
  • 2020-11-28 13:00

    No, constructors are not at all called if you use Serialization process. Serializable uses reflection to construct object and does not require no arg constructor. But Externalizable requires public no-arg constructor. however, you parent class may require no-arg constructor.refer this article

    An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the first superclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime.

    you wouldn't get this error if you make the parent class serializable or if you provide public no-arg constructor in parent class.

    0 讨论(0)
  • 2020-11-28 13:01

    Add implementation of Serializable to parent class.

    0 讨论(0)
  • 2020-11-28 13:19

    Just provide default constructor in both classes (Parent & Child)

    During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream. more

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