Java静态序列化规则? [英] Java static serialization rules?

查看:107
本文介绍了Java静态序列化规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些静态方法和字段进行保存状态序列化。我可以发誓虽然序列化和静态造成了混乱。我应该使所有静态瞬态?并且会使通话膨胀恢复静态正常吗?

I'm working on a save state serialization with a few static methods and fields. I could have sworn though that serialization and static's caused mayhem. Should I make all static's transient? And will inflating the calls restore the statics as normal?

推荐答案

静态 s隐含地 transient ,因此您不需要声明它们。

statics are implicitly transient, so you don't need to declare them as such.

序列化用于序列化 instances ,而不是 classes static 字段(方法无关紧要,因为它们是类定义的一部分,因此它们未被序列化)将重新初始化为加载类时设置的任何值。

Serialization is for serializing instances, not classes. static fields (methods are irrelevant since they are part of the class definition so they aren't serialized) will be reinitialized to whatever value they are set to when the class is loaded.

如果你有一个可变的 static 字段,那么对该值的更改将会丢失。

If you have a mutable static field, then the changes made to that value will be lost.

这篇关于Java静态序列化规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆