Question

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

Était-ce utile?

La solution

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.

Autres conseils

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

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).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top