UID的串行版本是什么? [英] What is a serial version UID used for?

查看:128
本文介绍了UID的串行版本是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Java应用程序,并且在创建要与ADT一起使用的接口时,发现需要初始化一个随机数作为ID号。

I'm creating a Java application, and when creating an interface to use with an ADT, it finds the need to initialize a random number as an ID number.

public class StackFullException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    public StackFullException(){}
    public StackFullException(String message) {
        super(message);
    }
}

我很好奇是否不这样做会影响与我的程序有关的任何事情,如果有的话,如何避免它。

I'm curious as to whether leaving this out will impact anything consequential about my program and if so, how to avoid it.

推荐答案

serialVersionUID 是Java序列化API的黑魔法的一部分。

The serialVersionUID is part of the black magic of the Java serialization API.

它用于唯一地标识类的版本,以便在删除类时-序列化版本可以与 ClassLoader 加载的类的版本进行检查。

It is used to uniquely identify a version of the class so that when a class is de-serialized the version can be checked against the version of the class loaded by the ClassLoader.

序列化API将如果未指定,则会生成一个 serialVersionUID 本身,但随后会因不重要的更改(或至少不破坏序列化兼容性的更改)而随机更改。

The serialization API will generate a serialVersionUID itself if none is specified but this is then subject to random change by inconsequential changes (or at least ones that don't break serialization compatibility).

您可以自己添加字段,从而可以控制此过程-您可以决定何时对类进行更改以打破旧版本的反序列化。

Adding the field yourself gives you control over this process - you decide when a change to the class should break de-serialization of older versions.

更多信息,请参见用于可序列化 的JavaDocs

More information can be found in the JavaDocs for Serializable.

简而言之,如果您打算序列化该类然后在以后对它进行反序列化-但在对代码进行了一些更改并重新编译后-此字段会更多-或几乎没有必要,以确保它可以按预期工作。

In short, if you plan to serialize this class and then de-serialize it later - but after making some changes to code and recompiling etc - this field is more-or-less essential to guarantee that this will work as intended.

这篇关于UID的串行版本是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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