Are static variables serialized in Serialization process

后端 未结 9 1631
深忆病人
深忆病人 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 13:11

    You can test this for yourself - here's some example code that should answer your question:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    class TestJava implements Serializable{
      public static int k = 10;
      public int j = 5;
    
      public static void main(String[] args) {
    
        TestJava tj1= new TestJava();
        TestJava tj2;
    
            try{ //serialization
                FileOutputStream fos = new FileOutputStream("myclass.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(tj1);
                oos.close();
                fos.close();
                System.out.println("object serielized 1..."+tj1.j);
                System.out.println("object serielized 2..."+tj1.k);
                System.out.println("object serielized 3..."+k);
                k=++k; // 'k' value incrementd after serialization
              } catch(FileNotFoundException fnfe){
                 fnfe.printStackTrace();
              } catch(IOException ioex){
                 ioex.printStackTrace();
              }
    
              try{ //deserialization
                  FileInputStream fis = new FileInputStream("myclass.ser");
                  ObjectInputStream ois = new ObjectInputStream(fis);
                  tj2 = (TestJava) ois.readObject();
                  ois.close();
                  fis.close();
                  System.out.println("object DEEEEserielized 1..."+tj2.j);
                  System.out.println("object DEEEEserielized 2..."+tj2.k); 
                  System.out.println("object DEEEEserielized 3..."+k); 
                // in deserialization 'k' value is shown as incremented. 
                // That means Static varialbe 'K' is not serialized.
                // if 'K' value is serialized then, it has to show old value before incrementd the 'K' value.
                } catch(FileNotFoundException fnfe){
                  fnfe.printStackTrace();
                } catch(IOException ioex){
                  ioex.printStackTrace();
                } catch(ClassNotFoundException CNFE){
                  CNFE.printStackTrace();                   
               }
          }
    }
    

    This will output the following:

    object serielized 1...5
    object serielized 2...10
    object serielized 3...10
    object DEEEEserielized 1...5
    object DEEEEserielized 2...11
    object DEEEEserielized 3...11
    

    So we create an object of class TestJava with one static integer field and one non-static field. We serialize the object, then - after serialization - increment the static integer.

    When we later deserialize the object, we see that it has the incremented value, implying that it was not serialized.

提交回复
热议问题