如何在Java中serialVersionUID序列化? [英] How is serialVersionUID serialized in Java?

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

问题描述

类成员(静态)无法序列化。原因很明显 - 它们不是由类的对象持有的。因为它们与类(而不是该类的对象)相关联,所以它们与对象分开存储。

Class members (static) cannot be serialized. The reason is obvious - they are not held by the object(s) of the class. Since they are associated with the class (rather than the object of that class), they are stored separately from the object.

serialVersionUID 被声明为实现 java.io.Serializable 界面类似于以下内容。

serialVersionUID is declared as a static field within a class that implements the java.io.Serializable interface something like the following.

private static final long serialVersionUID = 1L;

它用作可序列化 class。如果没有明确声明,将由JVM基于 Serializable 类的各个方面自动完成,如 Java(TM)对象序列化规范

It is used as a version control in a Serializable class. If it is not explicitly declared, will be done automatically by JVM, based on various aspects of the Serializable class, as described by the Java(TM) Object Serialization Specification.

如果未在实现 Serializable 接口的类中明确声明,则可能会发出警告。

If it is not explicitly declared within the class implementing the Serializable interface then a warning may issue.


可序列化类 SomeClass 不声明 static
serialVersionUID 字段类型 long

The serializable class SomeClass does not declare a static final serialVersionUID field of type long

即使它是 static ,它是序列化的,它是序列化的例外吗?

Is it serialized even though it is static, how or is it an exception to serialization?

推荐答案

序列化是神奇地完成的,有很多反思,并有各种特殊的行为 - 包括例如查找类的 static serialVersionUID

Serialization is done "magically," with lots of reflection, and has all sorts of special behavior -- including e.g. looking up the static serialVersionUID of the class.

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

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