Array classes with serialVersionUID?

↘锁芯ラ 提交于 2020-01-02 23:12:12

问题


I am having trouble understanding this comment from the Java serialization documentation:

Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.

Maybe I am unable to understand the obvious, however, I haven't figured why would I need to do this?


回答1:


I think that the answer to your question is to read up on serialVersionUID. In particular, you often want to specify this on your classes to be able to deal with the fact that classes change over time.

The comment that you are referring to is saying that you cannot set a serialVersionUID on arrays. If this was true, and the serialVersionUID always needed to match, then your array would have serious issues, and would be very fragile with respect to serialization. To counter this, the language designers decided that array (de)serialization would ignore the serialVersionUID, thus side-stepping the problem that it cannot be defined.




回答2:


I haven't figured why I would need to do his.

You don't. You can't. There is no syntax for defining array classes, let alone their serialVersionUID members.

That's also why checking it would be completely pointless. There is nothing about the class that could possibly change.



来源:https://stackoverflow.com/questions/19233303/array-classes-with-serialversionuid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!