Why does Java have transient fields?

前端 未结 15 2142
旧时难觅i
旧时难觅i 2020-11-22 03:54

Why does Java have transient fields?

15条回答
  •  余生分开走
    2020-11-22 04:29

    Because not all variables are of a serializable nature.

    1. Serialization and Deserialization are symmetry processes, if not you can't expect the result to be determined, in most cases, undetermined values are meaningless;
    2. Serialization and Deserialization are idempotent, it means you can do serialization as many time as you want, and the result is the same.

    So if the Object can exists on memory but not on disk, then the Object can't be serializable, because the machine can't restore the memory map when deserialization. Fro example, you can't serialize a Stream object.

    You can not serialize a Connection object, because it's state also dependent on the remote site.

提交回复
热议问题