"Serializable" classes should have a version id
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了"Serializable" classes should have a version id相关的知识,希望对你有一定的参考价值。
A serialVersionUID
field is required in all Serializable
classes. If you do not provide one, one will be calculated for you by the compiler. The danger in not explicitly choosing the value is that when the class changes, the compiler will generate an entirely new id, and you will be suddenly unable to deserialize (read from file) objects that were serialized with the previous version of the class.
serialVersionUID
‘s should be declared with all of these modifiers: static final long
.
Noncompliant Code Example
public class Raspberry extends Fruit // Noncompliant; no serialVersionUID. implements Serializable { private String variety; public Raspberry(Season ripe, String variety) { ...} public void setVariety(String variety) {...} public String getVarity() {...} } public class Raspberry extends Fruit implements Serializable { private final int serialVersionUID = 1; // Noncompliant; not static & int rather than long
Compliant Solution
public class Raspberry extends Fruit implements Serializable { private static final long serialVersionUID = 1; private String variety; public Raspberry(Season ripe, String variety) { ...} public void setVariety(String variety) {...} public String getVarity() {...} }
Exceptions
Swing and AWT classes, abstract
classes, Throwable
and its subclasses (Exception
s and Error
s), and classes marked with @SuppressWarnings("serial")
are ignored.
以上是关于"Serializable" classes should have a version id的主要内容,如果未能解决你的问题,请参考以下文章
java Serializable接口没有功能,为啥会影响“writeObject”/“readObject”
Intent.putExtra(String,Serializable)出错,请大家帮忙看看
spark2.1注册内部函数spark.udf.register("xx", xxx _),运行时抛出异常:Task not serializable
java程序中加入@SuppressWarnings("serial")是什么意思?