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?

Was it helpful?

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top