Are static variables serialized in Serialization process

后端 未结 9 1629
深忆病人
深忆病人 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.

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

    Yes, static variable will be serialized if it is initialized at the time of declaration.

    For example,

    case 1 : without initialization at the time of declaration

    class Person implements Serializable{
    
      public String firstName;
    
      static  String lastName;  
    }
    
    public class Employee {
    
      public static void main(String[] args) {
    
          Person p = new Person();
          p.firstName="abc";
          p.lastName="xyz";
          //to do serialization
    
      }
    
    }
    

    output :

    //after deserialization
    
     firstName= abc
    
     lastName= null
    

    case 2 : with initialization at the time of declaration

    class Person implements Serializable{
    
      public String firstName=="abc";
    
      static  String lastName="pqr";  
    }
    
    public class Employee {
    
      public static void main(String[] args) {
    
          Person p = new Person();
          p.firstName="abc";
          p.lastName="xyz";
          //to do serialization
    
      }
    
     }
    

    output :

    //after deserialization

    firstName= abc
    
    lastName= pqr
    
    0 讨论(0)
  • 2020-12-04 13:13

    No, if a class have static variable then at the time of serialization that variable will be skipped . because static variable is unique for all object and serialization is used for only save the object properties ( state of object ). static variable is a property of class

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