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:
引用参考: