Java serialization, UID not changed. Can I add new variables and method to the class?

前端 未结 4 1008
深忆病人
深忆病人 2020-12-02 19:27

I have a class that is serialised. Now I need to add a new variable into the class, with setter and getter methods. This class is sent over wire in RMI.

Without chan

4条回答
  •  悲&欢浪女
    2020-12-02 19:39

    Want to define few point to highlight the changes which impacts serialization. Below you will find the link to Oracle Java Docs for more details.

    Incompatible Changes

    Incompatible changes to classes are those changes for which the guarantee of interoperability cannot be maintained. The incompatible changes that may occur while evolving a class are:

    1. Deleting fields
    2. Moving classes up or down the hierarchy
    3. Changing a nonstatic field to static or a nontransient field to transient
    4. Changing the declared type of a primitive field
    5. Changing the writeObject or readObject method so that it no longer writes or reads the default field data or changing it so that it attempts to write it or read it when the previous version did not.
    6. Changing a class from Serializable to Externalizable or vice versa.
    7. Changing a class from a non-enum type to an enum type or vice versa.
    8. Removing either Serializable or Externalizable.
    9. Adding the writeReplace or readResolve method to a class, if the behavior would produce an object that is incompatible with any older version of the class.

    Link from where the above information is taken http://docs.oracle.com/javase/7/docs/platform/serialization/spec/version.html#6678

提交回复
热议问题