仅更改方法时的本地类不兼容错误 [英] Local class incompatible error when only a method has changed

查看:178
本文介绍了仅更改方法时的本地类不兼容错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚遇到了"WTF" 这类错误:我更新了我的类的一种方法,也添加了一种方法.运行我的程序后,当程序尝试打开并反序列化最近保存的数据时(在方法更改之前),这是我们突然想到的事情:

I just came across "WTF" sort of error: I updated one of my classe's methods and added one method too. After running my program, this is what popped our when program tried to open and unserialize recently saved data (before the methods changes):

java.io.InvalidClassException: cz.autoclient.settings.Settings; local class incompatible: stream classdesc serialVersionUID = 2404650814140543454, local class serialVersionUID = 4256355437298300223

根据Java文档所说,java方法没有被序列化.那么为什么serialVersionUID还要考虑类方法呢?

According to what java documentation says about that, java methods are not being serialized. So why does the serialVersionUID take class methods in account too?

由于Java程序员似乎真的对在各处使用getter和setter感到非常恼火,为什么不能为serialVersionUID创建getter以便我可以实现自己的仅计算属性的算法?

Since Java programmers seem to be so mad about using getters and setters literally everywhere, why is it not possible to create getter for serialVersionUID so that I can implement my own algorithm that only calculates the properties?

serialVersionUID可以被覆盖,但只有static final long serialVersionUID值,这需要我记住在更改类的属性时进行更改.

The serialVersionUID can be overriden, but only with static final long serialVersionUID value which would require me to remember to change it when I change class's properties.

推荐答案

基于Java文档,他们建议尽可能使用自定义serialVersionUID,因为默认算法会采用类实现的详细信息,并且结果是据说从JVM实现到其他实现都有所不同.

Based on the documentation for java, they recommend to use custom serialVersionUID as much as possible, because the default algorithm will take class implementation details and the result is said to vary from JVM implementation to another.

使用了默认算法 Java生成serialVersionUID的方法似乎也正在考虑非私有方法(步骤7).如果您在实现中使用默认的serialVersionUID,则会解释该异常.

The default algorithm used by Java to generate the serialVersionUID seems to be considering the non-private methods (step 7) as well. Which explains the exception if you were using the default serialVersionUID in your implementation.

正如您所建议的,如果我们可以拥有自己的方法实现来做到这一点,而不是将serialVersionUID覆盖为static final long,那就太好了.但是我想他们不允许这样做,因为如果允许的话,这种方法的错误实现可能会使serialVersionUID的整个目的无效.

As you suggested, it would be great if we can have our own implementation of a method to do this, rather than overriding the serialVersionUID as a static final long. But I guess they don't allow that because if it were allowed, wrong implementations of such a method could invalidate the whole purpose of the serialVersionUID.

这篇关于仅更改方法时的本地类不兼容错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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