Are static variables serialized in Serialization process

后端 未结 9 1628
深忆病人
深忆病人 2020-12-04 12:30

I\'m stumbled upon understanding java serialization. I have read in many documents and books that static and transient variables cannot be serialized in Java. We declare a

相关标签:
9条回答
  • 2020-12-04 12:48

    Below example explains about static ,instance,transient and super class varialbes serialization and their outputs.

    Serializing class:

    public class SerializeEx extends SuperSerializeEx implements Serializable {
    
        private static final long serialVersionUID = 1L;
        public static int staticNumber = 1234;
        public int instanceNumber = 1234;
    
        public SerializeEx() {
            staticNumber = 0;
            instanceNumber = 0;
            System.out.println("---sub class constructor---");
        }
    
        public SerializeEx(int staticNumber, int instanceNumber, int superNumber) {
            super(superNumber);
            this.staticNumber = staticNumber;
            this.instanceNumber = instanceNumber;
        }
    }
    

    Super Class:

    public class SuperSerializeEx {
    
        public int superNumber;
    
        public SuperSerializeEx() {
            System.out.println("---super class constructor---");
            this.superNumber = 1000;
        }
    
        public SuperSerializeEx(int superNumber) {
            this.superNumber = superNumber;
        }
    }
    

    Serialization & Deserialization:

    public class MainSerialization {
    
        public static void main(String[] args) {
            String fileName = "testing.txt";
            serialize(fileName);
            deSerialize(fileName);
        }
    
        public static void serialize(String fileName) {
            System.err.println("Serialize.....");
            SerializeEx serializeMe = new SerializeEx(10, 10, 10);
            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                fos = new FileOutputStream(fileName);
                out = new ObjectOutputStream(fos);
                out.writeObject(serializeMe);
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public static void deSerialize(String fileName) {
            System.err.println("DeSerialize.....");
            SerializeEx time = null;
            FileInputStream fis = null;
            ObjectInputStream in = null;
            try {
                fis = new FileInputStream(fileName);
                in = new ObjectInputStream(fis);
                time = (SerializeEx) in.readObject();
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            System.err.println("Instance Numer = " + time.instanceNumber + " \tStatic Number= " + time.staticNumber + " \t Super Number= " + time.superNumber);
            SerializeEx serializeMe = new SerializeEx(1001, 1001, 1001); //Modifying the static and instnce variables
            System.err.println("Instance Numer = " + time.instanceNumber + " \tStatic Number= " + time.staticNumber + " \t Super Number= " + time.superNumber);
        }
    }
    

    Output:

    ---super class constructor---
    Serialize.....
    DeSerialize.....
    Instance Numer = 10     Static Number= 10      Super Number= 1000
    Instance Numer = 10     Static Number= 1001    Super Number= 1000
    
    0 讨论(0)
  • 2020-12-04 12:50

    Any static variable which has been initialised at the time of declaration will be serialized.

    0 讨论(0)
  • 2020-12-04 12:54
    1. Instance Variables: These variables are serialized, so during deserialization we will get back the serialized state.

    2. Static Variables: These variables are not serialized, So during deserialization static variable value will loaded from the class.(Current value will be loaded.)

    3. transient Variables: transient variables are not serialized, so during deserialization those variables will be initialized with corresponding default values (ex: for objects null, int 0).

    4. Super class variables: If super class also implemented Serializable interface then those variables will be serialized, otherwise it won't serialize the super class variables. and while deserializing, JVM will run default constructor in super class and populates the default values. Same thing will happen for all superclasses.

    0 讨论(0)
  • 2020-12-04 12:57

    serialVersionUID is special and is not subject to these rules. There is code within the serialization machinery that specifically handles this field to perform the automatic version checks.

    0 讨论(0)
  • 2020-12-04 13:03

    The serialVersionUID is also serialized in this case.

    Any static variable that is provided a value during class initialization is serialized.

    However in normal cases, where you would provide the value to a static variable at the main class / run-time would not be serialized.

    You can try to access the serialVersionUID by making it public and try to access it after deserialization.

    You can refer "http://javabeginnerstutorial.com/core-java-tutorial/transient-vs-static-variable-java/" for more information.

    Hope that helps. Cheers !!

    0 讨论(0)
  • 2020-12-04 13:05

    serialVersionUID is a special static variable used by the serialization and deserialization process, to verify that a local class is compatible with the class used to serialize an object. It's not just a static variable as others, which are definitely not serialized.

    When an object of a class is first serialized, a class descriptor containing among other things the class name and serial version UID is written to the stream. When this is deserialized, the JVM checks if the serial version UID read from the stream is the same as the one of the local class. If they're not, it doesn't even try to deserialize the object, because it knows the classes are incompatible.

    0 讨论(0)
提交回复
热议问题