GVKun编程网logo

警告:[serial]可序列化的类SomeClass没有serialVersionUID的定义(可序列化的类被标记为)

9

如果您想了解警告:[serial]可序列化的类SomeClass没有serialVersionUID的定义的相关知识,那么本文是一篇不可错过的文章,我们将对可序列化的类被标记为进行全面详尽的解释,并且

如果您想了解警告:[serial]可序列化的类SomeClass没有serialVersionUID的定义的相关知识,那么本文是一篇不可错过的文章,我们将对可序列化的类被标记为进行全面详尽的解释,并且为您提供关于Android:如果我将serialVersionUID添加到旧的可序列化对象会怎样?、ArrayList中字段serialVersionUID和序列化的学习、idea 实现序列化 Serializable 接口,提示生成 serialVersionUID、Java 基础篇 - Serializable 与 serialVersionUID 的简单说明的有价值的信息。

本文目录一览:

警告:[serial]可序列化的类SomeClass没有serialVersionUID的定义(可序列化的类被标记为)

警告:[serial]可序列化的类SomeClass没有serialVersionUID的定义(可序列化的类被标记为)

尽管此类并非旨在被设置为可序列化的类,但我仍收到此异常警告!有人可以让我知道你是否遇到过这样奇怪的警告?

C:\Documents and Settings\...filename.java:60: warning: [serial] serializable class SomeClass has no definition of serialVersionUIDpublic class NewPortalConnection extends javax.swing.JFrame {

问候

答案1

小编典典

当您从实现Serializable的类派生时,将出现此警告。在您的情况下,Serializable父类是JFrame。

您可以使用@SuppressWarnings(“ serial”)禁止显示此警告,也可以为该类指定一个serialVersionUID:(privatestatic final long serialVersionUID = ...;点的长值)。

Android:如果我将serialVersionUID添加到旧的可序列化对象会怎样?

Android:如果我将serialVersionUID添加到旧的可序列化对象会怎样?

如果您获取一个从未明确指定serialVersionUID的旧可序列化对象,然后将serialVersionUID添加到该对象,会发生什么?在我看来,下一次由最终用户更新应用程序时,它将尝试反序列化磁盘中的数据,发现serialVersionUID不匹配,用服务器/
db /中的新数据覆盖数据,然后再执行此操作很好。我对这个假设是否正确?在执行此操作时,我是否还要警惕其他问题?

private class X implements serializable {...

private static final long serialVersionUID = 0L;

ArrayList中字段serialVersionUID和序列化的学习

ArrayList中字段serialVersionUID和序列化的学习

一个类的二进制字节序列转为java对象,也就是反序列化时,JVM会把传进来的二进制字节流中的serialVersionUID和本地相应的实体或对象的serialVersionUID进行比较,如果相同,则认为两个类是一致的,可以进行反序列化,否则就会出现版本不一致的反序列化异常。

添加private static final long 类型的字段serialVersionUID,格式如下:

文件中的方法和属性为该类自动生成一个serialVersionUID。只要该类的属性和方法不改变,则serialVersionUID不会改变。

方法和属性敏感,只要类中的属性or方法发生改变,则再次编译的class文件对应的serialVersionUID会与之前生成的不一致。如果之前生成的java实例已经被导入到磁盘进行了存储,那么被反序列化时,会出现异常。

显示定义的serialVersionUID一直不会改变,则不会出现上述异常。

class TestSeriable implements Externalizable {
public transient String name;
public String pwd;
public int age;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}
public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
//序列化对象
@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(name);
    out.writeObject(pwd);
    out.writeInt(age);
}

//反序列化对象
@Override
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {
    name=(String)in.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();
    pwd=(String)in.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();
    age=in.readInt();
}
//被序列化的类:必有<a href="https://www.jb51.cc/tag/publiclei/" target="_blank">public类</a>型的无参构造器。因反序列化时,先<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank">调用</a>此<a href="https://www.jb51.cc/tag/fangfa/" target="_blank">方法</a>实例化<a href="https://www.jb51.cc/tag/yige/" target="_blank">一个</a>类,再<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank">调用</a>readExternal<a href="https://www.jb51.cc/tag/fangfa/" target="_blank">方法</a>读取<a href="https://www.jb51.cc/tag/shuxing/" target="_blank">属性</a>值。
public TestSeriable(){
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable ts=new TestSeriable();
ts.setName("cxh");
ts.setPwd("123456");
ts.setAge(3);
//序列化
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test"));
out.writeObject(ts);

    //反序列化
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("test"));
    ts = (TestSeriable)in.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();

    //<a href="https://www.jb51.cc/tag/shuchu/" target="_blank">输出</a>:
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("name:"+ts.name);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("pwd:"+ts.pwd);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("age:"+ts.age);

    //<a href="https://www.jb51.cc/tag/guanbi/" target="_blank">关闭</a>连接
    out.close();
    in.close();
}

}


<span>输出结果:
<code><span>name:cxh
pwd:123456
age:3

Process finished with exit code 0


<span>

生成serialVersionUID ,只要类的属性和方法不改变,则导出字节码就可以被反序列化

class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private transient String pwd;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");
test.setPwd("123456");

    ObjectOutputStream  oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
    oos.writeObject(test);
    oos.close();

    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt"));
    TestSeriable ts=(TestSeriable) ois.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();//rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject返回final类型的Object对象,需要强制转化
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my name:"+ts.getName());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my age:"+ts.getAge());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my pwd:"+ts.getPwd());


}

}


<span>
输出结果:

Process finished with exit code 0

<span>

方法发生改变,则原来导出的字节码不能被反序列化。

添加字段school。

class TestSeriable implements Serializable {
private String name;
private int age;
private transient String pwd;

private String school;//新增字段

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}
public String getSchool() {
    return school;
}

public void setSchool(String school) {
    this.school = school;
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");
test.setPwd("123456");

// ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
// oos.writeObject(test);
// oos.close();

    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt"));
    TestSeriable ts=(TestSeriable) ois.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();//rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject返回final类型的Object对象,需要强制转化
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my name:"+ts.getName());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my age:"+ts.getAge());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my pwd:"+ts.getPwd());


}

}


<span>报错信息:

dobject0(ObjectInputStream.java:1535)
    at java.io.ObjectInputStream.readobject(ObjectInputStream.java:422)
    at Main.main(Main.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1


<span>

属性或者方法发生更改后,仍然能进行反序列化。

class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
public static long getSerialVersionUIDs() {
    return serialVersionUIDs;
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");

    ObjectOutputStream  oos=new ObjectOutputStream(new FileOutputStream("file1.txt"));
    oos.writeObject(test);
    oos.close();

    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file1.txt"));
    TestSeriable ts=(TestSeriable) ois.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();//rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject返回final类型的Object对象,需要强制转化
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my name:"+ts.getName());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my age:"+ts.getAge());
}

}

输出结果:

Process finished with exit code 0

<span>

class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private String address;//新增字段

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
//新增<a href="https://www.jb51.cc/tag/fangfa/" target="_blank">方法</a>
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");

    ObjectOutputStream  oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
    oos.writeObject(test);
    oos.close();

    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt"));
    TestSeriable ts=(TestSeriable) ois.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();//rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject返回final类型的Object对象,需要强制转化
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my name:"+ts.getName());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my age:"+ts.getAge());
}

}


<span>
输出结果:

Process finished with exit code 0

<span>

一个变量为static修饰后,不管是否被transient修饰,均不能被序列化。

class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private static String address;//更改类型为static字段

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
//新增<a href="https://www.jb51.cc/tag/fangfa/" target="_blank">方法</a>
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");
test.setAddress("海淀");

    ObjectOutputStream  oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
    oos.writeObject(test);
    oos.close();

// ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt"));
// TestSeriable ts=(TestSeriable) ois.readobject();//readobject返回final类型的Object对象,需要强制转化
// System.out.println("my name:"+ts.getName());
// System.out.println("my age:"+ts.getAge());
// System.out.println("my address:"+ts.getAddress());
}
}

class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private static String address;//更改类型为static字段

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
//新增<a href="https://www.jb51.cc/tag/fangfa/" target="_blank">方法</a>
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
// TestSeriable test=new TestSeriable();
// test.setAge(27);
// test.setName("cxh");
// test.setAddress("海淀");
//
// ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
// oos.writeObject(test);
// oos.close();

    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt"));
    TestSeriable ts=(TestSeriable) ois.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject();//rea<a href="https://www.jb51.cc/tag/dob/" target="_blank">dob</a>ject返回final类型的Object对象,需要强制转化
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my name:"+ts.getName());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my age:"+ts.getAge());
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("my address:"+ts.getAddress());
}

}


<span>
输出结果:

Process finished with exit code 0

<span>

idea 实现序列化 Serializable 接口,提示生成 serialVersionUID

idea 实现序列化 Serializable 接口,提示生成 serialVersionUID

首先 Ctrl+Alt+S,打开系统设置。 选择如下选项:

在所选中的选项中,打上勾即可。

提示:

Java 基础篇 - Serializable 与 serialVersionUID 的简单说明

Java 基础篇 - Serializable 与 serialVersionUID 的简单说明

一直觉得学习一手的知识,能更好的理解一些问题的来龙去脉,本文对 Java 文档中 Serializable 与 serialVersionUID 相关的描述进行了粗略的翻译,以帮助大家更好的理解。

 

原文:Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

译文:所有实现 java.io.Serializable 接口的类都是可串行化的。没有实现这个接口的类将不具备任何序列化或反序列化的状态。可序列化类的所有子类都是可序列化的。 序列化接口没有方法或字段,并且仅用于标识可序列化的语义。

 

原文:To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype''s public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class''s state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

译文:如需对非序列化类的子类进行序列化,子类需要拓展了一个可见的无参构建函数进行该类状态的初始化(即承担保存与恢复父类中所有 public、protected 及 package 作用域变量的责任)。否则,在运行时将报错。

 

原文:During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

译文:在反序列化过程中,不可序列化类的变量将使用该类的公共或受保护的无参数构造函数来初始化。无参数构造函数必须可以被可序列化的子类访问。可序列化子类的字段将从流中恢复。

 

原文:When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

译文:进行遍列时,可能会遇到不支持可序列化接口的类。在这种情况下,将抛出 NotSerializableException 用以标识该类的不可序列化的对象。


原文:Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

译文:如需要对类的序列化与反序列化进行特殊处理,则需要实现以下几个特殊函数。

 

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;
 private void readObjectNoData()
     throws ObjectStreamException;
 

原文:The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object''s fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

译文:writeObject 方法负责为其特定类编写对象的状态,以便相应的 readObject 方法可以恢复它。保存对象的字段的默认机制可以通过调用 out.defaultWriteObject 来调用。该方法不需要关心属于其超类或子类的状态。通过使用 writeObject 方法或通过使用 DataOutput 支持的原始数据类型的方法将各个字段写入 ObjectOutputStream 来保存状态。

 

原文:The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object''s non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

译文:readObject 方法负责从流中读取和恢复类字段。 它可以调用 in.defaultReadObject 来调用恢复对象的非静态和非瞬态字段的默认机制。 defaultReadObject 方法使用流中的信息来将流中保存的对象的字段分配给当前对象中相应命名的字段。这处理当类已经进化以添加新字段时的情况。该方法不需要关心属于其超类或子类的状态。 通过使用 writeObject 方法或通过使用 DataOutput 支持的原始数据类型的方法将各个字段写入 ObjectOutputStream 来保存状态

 

原文:The readObjectNoData method is responsible for initializing the state of the object for its particular class in the event that the serialization stream does not list the given class as a superclass of the object being deserialized. This may occur in cases where the receiving party uses a different version of the deserialized instance''s class than the sending party, and the receiver''s version extends classes that are not extended by the sender''s version. This may also occur if the serialization stream has been tampered; hence, readObjectNoData is useful for initializing deserialized objects properly despite a "hostile" or incomplete source stream.

译文:readObjectNoData 方法负责在序列化流不将给定类列为被反序列化的对象的超类的情况下,为其特定类初始化对象的状态。这可能发生在接收方使用与发送方不同的反序列化实例的类的版本,并且接收方的版本扩展了未被发送方的版本扩展的类的情况下。如果串行化流已经被篡改,这也可能发生;因此,readObjectNoData 对于正确初始化反序列化对象非常有用,尽管有 “敌意” 或不完整的源流。

 

原文:Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature:

译文:在将对象写入流时,需要指定一个替代对象的可序列化类应该使用具有特定签名的特殊方法:

 ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
 
原文:This writeReplace method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.
译文:如果该方法存在,并且它可以从正在序列化的对象的类中定义的方法访问,则通过序列化调用此 writeReplace 方法。因此,该方法可以具有私有,受保护和包私有访问。子类对此方法的访问遵循 Java 可见性规则。

 

原文:Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signature.

译文:当从流中读取实例时,需要指定替换的类应该使用确切的签名来实现此特殊方法。

 ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 

原文:This readResolve method follows the same invocation rules and accessibility rules as writeReplace.

译文:此 readResolve 方法遵循与 writeReplace 相同的调用规则和可见性规则。

 

原文:The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender''s class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

译文:序列化运行时与每个可序列化类相关联的版本号,称为 serialVersionUID,其在反序列化期间用于验证序列化对象的发送方和接收方已经加载了针对该序列化兼容的对象的类。 如果接收者加载了一个对象的类,该类的 serialVersionUID 不同于对应的发送者类的 serialVersionUID,那么反序列化将导致 InvalidClassException。 可序列化类可以通过声明一个名为 “serialVersionUID” 的字段来显式声明它自己的 serialVersionUID,该字段必须是 static,final,类型为 long:

 

 ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
 

原文:If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java (TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.

译文:如果可序列化类没有显式地声明 serialVersionUID,则串行化运行时将基于类的各个方面计算该类的默认 serialVersionUID 值,如 Java(TM)对象序列化规范中所述。但是,强烈建议所有可序列化类显式声明 serialVersionUID 值,因为缺省的 serialVersionUID 计算对类详细信息非常敏感,可能会因编译器实现而异,因此可能会导致反序列化期间出现意外的 InvalidClassExceptions。因此,为了确保不同 Java 编译器实现中的 serialVersionUID 值一致,可序列化类必须声明一个显式的 serialVersionUID 值。还强烈建议,显式 serialVersionUID 声明在可能的情况下使用 private 修饰符,因为这样的声明仅适用于立即声明的类 - serialVersionUID 字段不作为继承成员使用。数组类不能声明显式 serialVersionUID,因此它们总是具有默认计算值,但对于数组类,可以不需要匹配 serialVersionUID 值。

关于警告:[serial]可序列化的类SomeClass没有serialVersionUID的定义可序列化的类被标记为的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android:如果我将serialVersionUID添加到旧的可序列化对象会怎样?、ArrayList中字段serialVersionUID和序列化的学习、idea 实现序列化 Serializable 接口,提示生成 serialVersionUID、Java 基础篇 - Serializable 与 serialVersionUID 的简单说明等相关内容,可以在本站寻找。

本文标签: