简体   繁体   English

static 不可变对象(字符串)和 static 可变对象(单例 class 对象)在 Z913F725F48742 中的序列化

[英]serialization of static immutable objects(String) and static mutable object(Singleton class objects) in java

i am a bit confuse about serialization of static variables as they cannot be serialization.我对 static 变量的序列化有点困惑,因为它们不能被序列化。 Although singleton class objects can be serialization.虽然 singleton class 对象可以被序列化。 i have a sample code:我有一个示例代码:

  1. in TestSerilization class: i first compiled an run the code without comments i got the result, but as i comment the part and recompile and run i didnot got the desired result for static String values while de-serializationTestSerilization class 中:我首先编译了一个没有注释的运行代码,我得到了结果,但是当我注释该部分并重新编译并运行时,我没有得到 static 字符串值在反序列化时所需的结果
  2. in TestSerilization2 class: i first compiled an run the code without comments i got the result, but as i comment the part and recompile and run i did got the same result for private static Student instance values while de-serialization.TestSerilization2 class 中:我首先编译了一个没有注释的运行代码,我得到了结果,但是当我评论该部分并重新编译和运行时,我确实得到了私有 static 学生实例值在反序列化时相同的结果。

    i want to ask that why the private static Student instance gave values but private static String name didn't, although both are 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());

        }
    }

You say that the " private static Student instance gave values" when serialized, and that the " private static String name didn't", but what you are comparing is different from what you think your are comparing.您说“ private static Student instance在序列化时给出了值”,并且“ private static String name没有”,但是您正在比较的内容与您认为正在比较的内容不同。 You seem to think that in the first example you saw values when serializing private static Student instance but didn't when serializing private static String name , but you never serialized private static String name .您似乎认为在第一个示例中,您在序列化private static Student instance时看到了值,但在序列化private static String name时没有看到值,但您从未序列化private static String name Actually, you saw values when serializing a Student instance with String name and didn't when serializing a Student instance with static String name .实际上,您在使用String name序列化Student实例时看到了值,而在使用static String name序列化Student实例时没有看到值。 To clarify,澄清,

  • In the 1st example, the name field IS static , and it IS NOT serialized.在第一个示例中, name字段static ,并且未序列化。
  • In the 2nd example, the name field IS NOT static , and it IS serialized.在第二个示例中, name字段不是static ,它是序列化的。

A field marked as static indicates that it is owned by the class, which is why they are referenced via the class ( MyClass.field ) rather than via an instance ( ((MyClass) instance).field ).标记为static的字段表示它由 class 拥有,这就是为什么它们通过 class ((MyClass) instance).field MyClass.field ) 而不是通过实例引用的原因。) Since you are dealing with Java object serialization, the static fields are not serialized as part of the object.由于您正在处理 Java object序列化,因此static字段未作为 ZA8Z6663391B4B6661 的一部分序列化

Whether or not a reference to an object is static is irrelevant when serializing that object.在序列化 object 时对 object 的引用是否为 static 无关紧要。 Ie Student student (non-static reference) will serialize just the same as static Student student (static reference).Student student (非静态引用)将与static Student student (静态引用)一样序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM