[英]serialization of static immutable objects(String) and static mutable object(Singleton class objects) in java
我对 static 变量的序列化有点困惑,因为它们不能被序列化。 虽然 singleton class 对象可以被序列化。 我有一个示例代码:
TestSerilization
class 中:我首先编译了一个没有注释的运行代码,我得到了结果,但是当我注释该部分并重新编译并运行时,我没有得到 static 字符串值在反序列化时所需的结果在TestSerilization2
class 中:我首先编译了一个没有注释的运行代码,我得到了结果,但是当我评论该部分并重新编译和运行时,我确实得到了私有 static 学生实例值在反序列化时相同的结果。
我想问为什么private static Student
实例给出了值,但私有static String name
没有,尽管两者都是static
。
import java.io.*;
class Student implements Serializable{
private static String name;
private int rollNo;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setRollNo(int rollNo){
this.rollNo=rollNo;
}
public int getRollNo(){
return this.rollNo;
}
}
public class TestSerilization{
public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{
/*
Student s1=new Student();
s1.setName("Apoorv");
s1.setRollNo(13);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\JAVA Practice\\output.ser"));
oos.writeObject(s1);
System.out.println("s1: name-"+s1.getName()+" rollno-"+s1.getRollNo()+" hascode: "+s1.hashCode());
*/
ObjectInputStream ins=new ObjectInputStream(new FileInputStream("D:\\JAVA Practice\\output.ser"));
Student s2=(Student)ins.readObject();
System.out.println("s2: name-"+s2.getName()+" rollno-"+s2.getRollNo()+" hascode: "+s2.hashCode());
}
}
import java.io.*;
final class Student implements Serializable{
private static Student instance;
private String name;
private int rollNo;
private Student(){}
public static Student getInstance(){
if(instance==null){
instance=new Student();
}
return instance;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setRollNo(int rollNo){
this.rollNo=rollNo;
}
public int getRollNo(){
return this.rollNo;
}
}
public class TestSerilization2{
public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{
/*
Student s1=Student.getInstance();
s1.setName("Apoorv");
s1.setRollNo(13);
System.out.println("s1: name-"+s1.getName()+" rollno-"+s1.getRollNo()+" hascode: "+s1.hashCode());
Student s2=Student.getInstance();
System.out.println("s2: name-"+s2.getName()+" rollno-"+s2.getRollNo()+" hascode: "+s2.hashCode());
*/
/*
Student s1=Student.getInstance();
s1.setName("Apoorv");
s1.setRollNo(13);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\JAVA Practice\\output2.ser"));
oos.writeObject(s1);
System.out.println("s1: name-"+s1.getName()+" rollno-"+s1.getRollNo()+" hascode: "+s1.hashCode());
*/
ObjectInputStream ins=new ObjectInputStream(new FileInputStream("D:\\JAVA Practice\\output2.ser"));
Student s2=(Student)ins.readObject();
System.out.println("s2: name-"+s2.getName()+" rollno-"+s2.getRollNo()+" hascode: "+s2.hashCode());
}
}
您说“ private static Student instance
在序列化时给出了值”,并且“ private static String name
没有”,但是您正在比较的内容与您认为正在比较的内容不同。 您似乎认为在第一个示例中,您在序列化private static Student instance
时看到了值,但在序列化private static String name
时没有看到值,但您从未序列化private static String name
实际上,您在使用String name
序列化Student
实例时看到了值,而在使用static String name
序列化Student
实例时没有看到值。 澄清,
name
字段static
,并且未序列化。name
字段不是static
,它是序列化的。 标记为static
的字段表示它由 class 拥有,这就是为什么它们通过 class ((MyClass) instance).field
MyClass.field
) 而不是通过实例引用的原因。) 由于您正在处理 Java object序列化,因此static
字段未作为 ZA8Z6663391B4B6661 的一部分序列化
在序列化 object 时,对 object 的引用是否为 static 无关紧要。 即Student student
(非静态引用)将与static Student student
(静态引用)一样序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.