查看一个实现Serializable接口的类序列化id
File-》settings,搜索seria
有的idea是下面的选项
点击apply后回到类所在位置,光标移到类名处 alt+回车
补充:序列化和反序列化过程
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -7758346360419873145L;
// 成员变量
String name;
// 用transient修饰,反序列化时该变量值就是默认值
transient int age;
// static修饰的变量不会被序列化到文件里(因为序列化是把对象数据进行持久化存储,而静态的属于类不属于对象)\
static int id;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
}
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
User user = new User("李四", 19);
User.id = 1111;
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("1.txt"));
// 实现序列化,把对象数据写到磁盘中
objectOutputStream.writeObject(user);
objectOutputStream.close();
//反序列化
// 创建从指定 InputStream 读取的 ObjectInputStream。
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("1.txt"));
// 从磁盘中读取对象数据
User user1 = (User) in.readObject();
System.out.println(user1);
// close
in.close();
}
}
可以看到静态变量没有序列化到文件里
修改User的成员变量,验证显示的id是否是该类的序列化id
修改后
相同