【关键字】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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值