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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:51:11  来源:igfitidea点击:

Java static serialization rules?

javaserializationstatictransient

提问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

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

statics 是隐式的transient,所以你不需要这样声明它们。

Serialization is for serializing instances, not classes. staticfields (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 staticfield, then the changes made to that value will be lost.

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

回答by Brett Kail

staticfields are ignored for serialization.

static序列化时忽略字段。

Updated to say staticrather than transientas I originally intended...

更新说static而不是transient我最初的意图......

回答by ColinD

staticfields 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. staticvariable are not saved during serialization. And on the contrary, during de-serialization process, the staticvariables are initiated from the class level initialization.

1.static序列化时不保存变量。相反,在反序列化过程中,static变量是从类级别初始化开始的。

2. staticand transientkeywords based variables are both ignored during serialization.

2.statictransient基于关键字的变量在序列化过程中都被忽略。

3. Class name and serialVersionUIDare both serialized as stream of bytes and when de-serialized the serialVersionUID, read from the source, is compared with local class same staticvariable. That is why serialVersionUIDis declared as static public finalso 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”文件的一部分。静态+瞬态与瞬态相同。