Java transient关键字使用小…

    xiaoxiao2021-04-19  154

    1、transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。 2、被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。 3、一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。也可以认为在将持久化的对象反序列化后,被transient修饰的变量将按照普通类成员变量一样被初始化。 如下面的例子 package com.kkoolerter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Date; public class Main implements Serializable {     private static final long serialVersionUID = -5836283489677344417L;     private transient int classValue = 10;     private transient Date date = new Date();     private transient static int staticValue = 10;     public static void main(String[] args) throws Exception {         Main m = new Main();         m.classValue = 11;         Main.staticValue = 11;         ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(                 new File("0xjh000")));         out.writeObject(m);         out.close();         ObjectInputStream in = new ObjectInputStream(new FileInputStream(                 new File("0xjh000")));         Main m1 = (Main) in.readObject();         in.close();         System.out.println(m1.classValue);         System.out.println((m1.date == null ? "date is null"                 : "date is not null"));     } } 程序将输出: 0 date is null 这就说明了一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。 思考一下下面的例子: package com.kkoolerter; import java.io.Externalizable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class ExternalizableTest implements Externalizable {     private transient String content = "哈哈~我将会被序列化,不管我是是否被transient关键字修饰";     @Override     public void writeExternal(ObjectOutput out) throws IOException {         out.writeObject(content);     }     @Override     public void readExternal(ObjectInput in) throws IOException,             ClassNotFoundException {         content = (String) in.readObject();     }     public static void main(String[] args) throws Exception {         ExternalizableTest et = new ExternalizableTest();         ObjectOutput out = new ObjectOutputStream(new FileOutputStream(                 new File("ext0000")));         out.writeObject(et);         ObjectInput in = new ObjectInputStream(new FileInputStream(new File(                 "ext0000")));         ExternalizableTest et1 = (ExternalizableTest) in.readObject();         System.out.println(et1.content);         out.close();         in.close();     } } 程序运行后将输出如下结果: 哈哈~我将会被序列化,不管我是是否被transient关键字修饰 这是为什么呢,不是说类的变量被transient关键字修饰以后将不能序列化了吗? 我们知道在Java中,对象的序列化可以通过实现两种接口来实现,若操作的是一个Serializable对象,则所有的序列化将会自动进行,若操作的是 一个Externalizable对象,则没有任何东西可以自动序列化,需要在writeExternal方法中进行手工指定所要序列化的变量,这与是否被transient修饰无关。因此第二个例子输出的是变量content初始化的内容,而不是null。

    本文出自 “有思想的代码” 博客,请务必保留此出处http://wujuxiang.blog.51cto.com/2250829/430211

    转载请注明原文地址: https://ju.6miu.com/read-676235.html

    最新回复(0)