问题
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