序列化和静态变量

时间:2018-11-23 10:53:54

标签: java serialization static

我读了一些有关序列化/反序列化的文章,但是我仍然无法弄清楚在这些过程中静态变量会发生什么。 here有一篇文章,它提供了一个代码示例,我在这里重写了该代码示例:

// Java code for serialization and deserialization 
// of a Java object 
import java.io.*; 

class Emp implements Serializable { 
private static final long serialversionUID = 129348938L; 
    transient int a; 
    static int b; 
    String name; 
    int age; 

    // Default constructor 
public Emp(String name, int age, int a, int b) 
    { 
        this.name = name; 
        this.age = age; 
        this.a = a; 
        this.b = b; 
    } 

} 

public class SerialExample { 
public static void printdata(Emp object1) 
    { 
        System.out.println("name = " + object1.name); 
        System.out.println("age = " + object1.age); 
        System.out.println("a = " + object1.a); 
        System.out.println("b = " + object1.b); 
    } 

public static void main(String[] args) 
    { 
        Emp object = new Emp("ab", 20, 2, 1000); 
        String filename = "shubham.txt"; 

        // Serialization 
        try { 

            // Saving of object in a file 
            FileOutputStream file = new FileOutputStream (filename); 
            ObjectOutputStream out = new ObjectOutputStream (file); 

            // Method for serialization of object 
            out.writeObject(object); 

            out.close(); 
            file.close(); 

            System.out.println("Object has been serialized\n"
                            + "Data before Deserialization."); 
            printdata(object); 

            // value of static variable changed 
            object.b = 2000; 
        } 

        catch (IOException ex) { 
            System.out.println("IOException is caught"); 
        } 

        object = null;

        // Deserialization 
        try { 

            // Reading the object from a file 
            FileInputStream file = new FileInputStream (filename); 
            ObjectInputStream in = new ObjectInputStream (file); 

            // Method for deserialization of object 
            object = (Emp)in.readObject(); 

            in.close(); 
            file.close(); 
            System.out.println("Object has been deserialized\n"
                                + "Data after Deserialization."); 
            printdata(object); 

            // System.out.println("z = " + object1.z); 
        } 

        catch (IOException ex) { 
            System.out.println("IOException is caught"); 
        } 

        catch (ClassNotFoundException ex) { 
            System.out.println("ClassNotFoundException is caught"); 
        } 
    } 
}

,输出为:

Object has been serialized
Data before Deserialization.
name = ab
age = 20
a = 2
b = 1000
Object has been deserialized
Data after Deserialization.
name = ab
age = 20
a = 0
b = 2000
简而言之,我们创建了一个Emp对象,将其写入文件中,然后关闭ObjectOutputStream和FileOutputStream,然后关闭后输出,我们更改了静态变量b的值,然后设置了< strong> OBJECT = NULL ,但是当我们在反序列化过程中从文件读取时,b的值是在写入文件后为其分配的!我的意思是关闭输出流,并且将原始对象也设置为null,那么我们怎么可能对b进行的更改保存在文件或程序字段中?!

0 个答案:

没有答案