java中静态不可变对象(字符串)和静态可变对象(单例类对象)的序列化

java中静态不可变对象(字符串)和静态可变对象(单例类对象)的序列化,java,serialization,singleton,Java,Serialization,Singleton,我对静态变量的序列化有点困惑,因为它们不能被序列化。虽然单例类对象可以序列化。 我有一个示例代码: 在testserialization类中:我首先编译了一个 注释我得到了结果,但当我注释部分并重新编译时 运行时,我没有得到静态字符串值所需的结果 反序列化时 在TestSerilization2类中:我首先编译了一个不带注释的运行代码的程序,我得到了结果,但在注释部分和 重新编译并运行我确实得到了与私有静态相同的结果 反序列化时的学生实例值 我想问一下为什么private static Stud

我对静态变量的序列化有点困惑,因为它们不能被序列化。虽然单例类对象可以序列化。 我有一个示例代码:

  • testserialization
    类中:我首先编译了一个 注释我得到了结果,但当我注释部分并重新编译时 运行时,我没有得到静态字符串值所需的结果 反序列化时
  • TestSerilization2
    类中:我首先编译了一个不带注释的运行代码的程序,我得到了结果,但在注释部分和 重新编译并运行我确实得到了与私有静态相同的结果 反序列化时的学生实例值

    我想问一下为什么
    private static Student
    实例给出了值,而private
    static String name
    没有给出值,尽管两者都是
    static


  • 私有静态学生实例在哪里
    ?为什么
    名称
    是静态的?(一般来说,除了静态常量之外,任何程序中都不应该有任何静态字段。)嗨,Andy,“私有静态学生实例”在Student中声明class@AndyTurner我已经在同一块写了两次学生代码。1.具有“私有静态字符串名称”的学生类,该名称不是单例设计,2。带有“私有静态学生实例”的学生类是单例设计。这两个类都需要单独运行。
    
        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());
    
            }
        }