Why is it legal to declare a transient variable in a non serializable class?

◇◆丶佛笑我妖孽 提交于 2019-12-24 13:39:15

问题


As for the subject title: Why is it legal to declare a transient variable in a non serializable class? What would the use be?


回答1:


The transient access modifier can be seen by code other than the serialization mechanism, and is used by some object databases to mark a data field as not persistent. Aside from that, there isn't any harm in allowing this.




回答2:


Because also other serialization forms that don't requirier Serializable are able to make use of it too.




回答3:


How about if a subclass implements Serializable?

In any case, it is impossible for the compiler to enforce this rule, i.e. emit a compile error
based on class hierarchy (except - of course - superclass defined methods).



来源:https://stackoverflow.com/questions/18040931/why-is-it-legal-to-declare-a-transient-variable-in-a-non-serializable-class

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