Java静态序列化规则?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6429462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java static serialization rules?
提问by ahodder
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?
我正在使用一些静态方法和字段进行保存状态序列化。我可以发誓虽然序列化和静态造成了混乱。我应该让所有静态的都是瞬态的吗?并且将调用膨胀恢复正常情况下的静力学?
采纳答案by Robin
static
s are implicitly transient
, so you don't need to declare them as such.
static
s 是隐式的transient
,所以你不需要这样声明它们。
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.
如果您有一个可变static
字段,那么对该值所做的更改将丢失。
回答by Brett Kail
static
fields are ignored for serialization.
static
序列化时忽略字段。
Updated to say static
rather than transient
as I originally intended...
更新说static
而不是transient
我最初的意图......
回答by ColinD
static
fields aren't serialized.
static
字段未序列化。
回答by Cris
"When you serialize an instance of a class, the only things that are saved are the non-static and non-transient instance data. Class definitions are not saved. They must be available when you try to deserialize an object" http://java.sun.com/developer/technicalArticles/ALT/serialization/
“当您序列化类的实例时,唯一保存的是非静态和非瞬态实例数据。类定义不会保存。当您尝试反序列化对象时,它们必须可用” http:// java.sun.com/developer/technicalArticles/ALT/serialization/
回答by Gagandeep
The short rules can be as follows:
简短的规则如下:
1. static
variable are not saved during serialization. And on the contrary, during de-serialization process, the static
variables are initiated from the class level initialization.
1.static
序列化时不保存变量。相反,在反序列化过程中,static
变量是从类级别初始化开始的。
2. static
and transient
keywords based variables are both ignored during serialization.
2.static
和transient
基于关键字的变量在序列化过程中都被忽略。
3. Class name and serialVersionUID
are both serialized as stream of bytes and when de-serialized the serialVersionUID
, read from the source, is compared with local class same static
variable. That is why serialVersionUID
is declared as static public final
so that no further object needs to be created for comparing these versionUID(s).
3. 类名 和serialVersionUID
都被序列化为字节流,反序列化时serialVersionUID
,从源读取的 与本地类相同static
变量进行比较。这就是为什么serialVersionUID
声明为static public final
不需要创建其他对象来比较这些 versionUID(s)。
- If in case any difference is found, then a InvalidClassExceptionwould occur.
- 如果发现任何差异, 则会发生InvalidClassException。
回答by Jatin Shashoo
Static fields can never be a part of "ser" file. Static + transient is the same as transient.
静态字段永远不能成为“ser”文件的一部分。静态+瞬态与瞬态相同。