【關鍵字】java中transient關鍵字的用法

 

java中transient關鍵字的用法其實不用多說,另外需要注意兩個問題,這裏簡單介紹用法及注意事項:

1、實現Serializable後使用transient修改時的字段不能序列化

    實現Serializable接口,使用transient修飾type變量,type字段將不會序列化

//實現Serializable後使用transient修改時的字段不能序列化
public class Car implements Serializable{

	private String name;
	
	private transient String type;

	public Car(String name, String type) {
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "Car [name=" + name + ", type=" + type + "]";
	}

}

測試用例

public class TransientTest {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Car car = new Car("xxxName","xxxType");
		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\1.txt")));
		os.writeObject(car);
		
//		car.setType("aaa");
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\1.txt")));
		Car object = (Car)in.readObject();
		
		System.out.println("object:"+object.toString());
		
	}
}

結果,未被序列化的字段,反序列化後爲null


2、使用static關鍵字也不能被序列化

將字段type改爲static修飾

private static String type;

運行測試代碼,發現type反序列化仍然有值:

static不能被序列化有誤?非也,因爲反序列化後類中static變量type的值爲當前JVM中對應type值,這個值是JVM中的不是反序列化得出的,如下實例說明問題:

public class TransientTest {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Car car = new Car("xxxName","xxxType");
		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\1.txt")));
		os.writeObject(car);
		
		//在car序列化後,將type設置爲其他值,查看反序列化後的值
		car.setType("aaa");
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\1.txt")));
		Car object = (Car)in.readObject();
		
		System.out.println("object:"+object.toString());
		
	}
}

結果如下,type值爲設置的aaa,足以說明問題,至於爲什麼,暫時沒有深究:


3、實現Externalizable重載後都能被序列化

public class Car implements Externalizable{
	private String name;
	//實現Serializable後使用transient修改時的字段不能序列化
	//使用static關鍵字也不能被序列化
	//實現Externalizable重載後都能被序列化
	private static String type;
	
    //實現Externalizable需要有空參構造器
	public Car() {
	}

	public Car(String name, String type) {
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "Car [name=" + name + ", type=" + type + "]";
	}
	
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		// TODO Auto-generated method stub
		out.writeObject(name);
		out.writeObject(type);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		this.name = (String)in.readObject();
		this.type = (String)in.readObject();
	}

}

測試:

public class TransientTest {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Car car = new Car("xxxName","xxxType");
		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\1.txt")));
		os.writeObject(car);
		
		//在car序列化後,將type設置爲其他值,查看反序列化後的值
		car.setType("aaa");
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\1.txt")));
		Car object = (Car)in.readObject();
		
		System.out.println("object:"+object.toString());
		
	}
}

結果,type依然爲最初設置值,不爲null也不爲aaa:

 

 

 

引用參考:

https://www.jianshu.com/p/056dc2a53773?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章