Loading

序列化与transient

java keyword: transient
usage: skip serialization for transient-field

import java.io.*;
class Test implements Serializable
{
    int i = 10;
  
    transient int k = 30;
  
    transient static int l = 40;

    public static void main(String[] args) throws Exception
    {
        Test input = new Test();
  
	// write the Test-class obj to the file
        FileOutputStream fos = new FileOutputStream("abc.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(input);
	// read to Test-class obj from the file
        FileInputStream fis = new FileInputStream("abc.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Test output = (Test)ois.readObject();
    }
}
// [OUT]:Test-obj output is { i = 10;k = 0;l = 40}
posted @ 2021-05-22 20:44  Xaf17  阅读(53)  评论(0)    收藏  举报