Why does Java have transient fields?

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

Why does Java have transient fields?

15条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 04:40

    A transient variable is a variable that may not be serialized.

    One example of when this might be useful that comes to mind is, variables that make only sense in the context of a specific object instance and which become invalid once you have serialized and deserialized the object. In that case it is useful to have those variables become null instead so that you can re-initialize them with useful data when needed.

提交回复
热议问题