一、transient关键字作用
transient是Java的关键字,用来表示一个域不是该对象序列化的一部分。当一个对象被序列化时,被声明为transient的变量的值将不包括在序列化表示中,非transient的变量才被包括进去。
二、transient关键字用途
如:网络操作用户信息时,我们并不希望用户的敏感信息(如证件号、密码等)在网络中被传输。我们可在相应属性上加上transient关键字可避免。被修饰了transient的属性生命周期只存在于内存中,并不会被持久化。
三、transient关键字的使用
在实现了java.io.Serializable接口的类上,将不需要序列化的属性前面增加关键字transient,序列化对象的时候,这个属性不会被序列化。
如:
private transient
SmallDog
otherHalf
;
四、实践
实现了java.io.Serializable接口的类 |
public class SmallDog implements Serializable{ private static final long serialVersionUID = 1L; private String name; private Double weight; private int age; private Set<SmallDog> childs; private transient SmallDog otherHalf; public static String test; public SmallDog() {} public SmallDog(String name, Double weight, int age) { this.name = name; this.weight = weight; this.age = age; test = "serialization static property test"; } public SmallDog(String name, Double weight, int age, Set<SmallDog> childs, SmallDog otherHalf) { this.name = name; this.weight = weight; this.age = age; this.childs = childs; this.otherHalf = otherHalf; test = "serialization static property test"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getWeight() { return weight; } public void setWeight(Double weight) { this.weight = weight; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Set<SmallDog> getChilds() { return childs; } public void setChilds(Set<SmallDog> childs) { this.childs = childs; } public SmallDog getOtherHalf() { return otherHalf; } public void setOtherHalf(SmallDog otherHalf) { this.otherHalf = otherHalf; } @Override public String toString() { return "SmallDog{" + "name='" + name + '\'' + ", weight=" + weight + ", age=" + age + ", otherHalf=" + (otherHalf == null ? "" : otherHalf.toString()) + ", childs=" + (childs == null ? "" : childs.toString()) + ", test=" + test + '}'; } } |
序列化代码片段 |
public void serializableTransientSmallDog(){ com.smalldog.transientproperty.SmallDog otherHalf = new com.smalldog.transientproperty.SmallDog("小红", 6.5, 3); Set<com.smalldog.transientproperty.SmallDog> childs = new HashSet<com.smalldog.transientproperty.SmallDog>(); childs.add(new com.smalldog.transientproperty.SmallDog("老大", 4.0, 1)); childs.add(new com.smalldog.transientproperty.SmallDog("老二", 4.5, 1)); com.smalldog.transientproperty.SmallDog smallDog = new com.smalldog.transientproperty.SmallDog("小白", 6.0, 3, childs, otherHalf); try { FileOutputStream fos = new FileOutputStream("transientTest.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(smallDog); oos.flush(); oos.close(); } catch (IOException e) { e.printStackTrace(); } } |
序列化后文件:
反序列化代码片段 |
public void deserializableTransientSmallDog(){ try { com.smalldog.transientproperty.SmallDog smallDog; FileInputStream fis = new FileInputStream(new File("transientTest.txt")); ObjectInputStream ois = new ObjectInputStream(fis); smallDog = (com.smalldog.transientproperty.SmallDog)ois.readObject(); System.out.println(smallDog.toString()); ois.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } |
输出结果 |
SmallDog{name='小白', weight=6.0, age=3, otherHalf=, childs=[SmallDog{name='老二', weight=4.5, age=1, otherHalf=, childs=, test=null}, SmallDog{name='老大', weight=4.0, age=1, otherHalf=, childs=, test=null}], test=null} |
五、结论
由以上例子可看出,被修饰了static与transient关键字的属性不会被序列化。例子中otherHalf属性对象是实现了java.io.Serializable接口的类,因被修饰了transient关键字,所以没有被序列化到硬盘文件中。
提示:在实现了java.io.Serializable接口的类中,若声明的属性对象没有实现java.io.Serializable接口,此时若这个类被序列化时,声明的这个属性对象也会被序列化。