"Serializable" classes should have a version id

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 (Exceptions and Errors), and classes marked with @SuppressWarnings("serial") are ignored.

时间: 2024-08-24 07:44:56

"Serializable" classes should have a version id的相关文章

Sonar 规则

bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. bug 主要 不要使用equals方法对AtomicXXX进行是否相等的判断 Atomic变量永远只会和自身相等,Atomic变量没有覆写equals()方法.2."=+" should not be used instead of "+=" bug 主要 "

持久化和序列化的关系

Hibernate Hibernate是轻量级Java EE应用的持久层解决方案,Hibernate不仅管理者Java类到数据库表的映射(包括Java 数据类型到SQL数据类型的映射),还提供数据查询和获取数据的方法,可以大幅度的缩短使用JDBC处理数据持久化的时间. 目前主流的数据库依然是关系型,如db2.oracle 对象关系数据库映射(ORM  object/relation mapping):Hibernate ORM,其作用就是去映射对象和关系型数据库的,以达到程序中的业务逻辑和数据访

java.io.InvalidClassException 异常解决, 实现Serializable接口的注意事项

解决方案: 在类中显式指定 private static final long serialVersionUID = 42L; 类实现序列化接口, 进行序列化反序列化的时候, 抛出 java.io.InvalidClassException 异常 java.io.InvalidClassException: com.xx.Xxx; local class incompatible: stream classdesc serialVersionUID = -783991920331, local

android 中传递对象两种方法探索(Serializable,Parcelable)

相信大家在android开发的过程中总会遇到要在Activity中间传递数据的情况,当然,遇到需要在Intent中传递对象的情况也不可避免,所以我就so了一下相关的知识,在这里总结消化一下.就目前来说,我了解到的只有两种方式: 1.利用Bundle.putSerializable(Key,Object): 2.利用Bundle.putParcelable(Key, Object): 下面详细介绍两种方法的使用和区别: 首先第一点,这两种方法实现的前提都需要将传递的对象Object序列化,那么,问

使用Serializable接口进行JAVA的序列化和反序列化

OBJECT STREAMS – SERIALIZATION AND DESERIALIZATION IN JAVA EXAMPLE USING SERIALIZABLE INTERFACE Hitesh Garg | November 7, 2014 | io | 9 Comments In the previous java tutorials I have discussed about basic of java streams, byte streams, then a modifie

Hibernate系列之ID生成策略

一.概述 hibernate中使用两种方式实现主键生成策略,分别是XML生成id和注解方式(@GeneratedValue),下面逐一进行总结. 二.XML配置方法 这种方式是在XX.hbm.xml文件中对generator进行配置,eg: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" &qu

关于Java Serial Version UID的一些说明

在网络传输Java对象.将Java对象存储到文件.将Java对象以BLOB形式存储到数据库中时,需要对Java对象进行序列化及反序列化,标准模式是实现Serializable接口.    实现上述接口时,需要提供一个Serial Version UID,该UID用于标识类的版本.一个对象被序列化后,只要其版本不变,都可以进行反序列化,一旦 改变造成版本不一致,会抛出InvalidClassException异常.    建议显示定义UID,如果不显示定义,JVM会自动产生一个值,这个值和编译器的

maven打包classes为jar

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.5</version> <configuration> <encoding>UTF-8</encoding> </configuration> <executi

android——Serializable &amp; Parcelable

Serializable & Parcelable这两种序列化方法是Android中经常使用的方法,Serializable是Android从Java中继承过来的,Parcelable是Android自己提供的方法,Google是推荐使用Parcelable,至于这两种方法的区别,下面通过对源码的分析来慢慢的了解. 在分析源码之前,首先还是说一下序列化在Android中使用的场景: 1)我们在四大组件之间使用intente来传递数据,intente中所传递的数据是需要序列化的 2)binder,