Java transient关键字使用小…

来源:互联网 发布:灵畅互动软件 编辑:程序博客网 时间:2024/06/11 03:44
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 {

    privatestatic final long serialVersionUID =-5836283489677344417L;
    privatetransient int classValue = 10;
    privatetransient Date date = new Date();
    privatetransient static int staticValue = 10;

    publicstatic void main(String[] args) throws Exception {
       Main m = newMain();
       m.classValue= 11;
      Main.staticValue = 11;
      ObjectOutputStream out = new ObjectOutputStream(newFileOutputStream(
             newFile("0xjh000")));
      out.writeObject(m);

      out.close();

      ObjectInputStream in = new ObjectInputStream(newFileInputStream(
             newFile("0xjh000")));
       Main m1 =(Main) in.readObject();
      in.close();

      System.out.println(m1.classValue);
      System.out.println((m1.date == null ? "date isnull"
             : "date isnot 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{

    privatetransient String content ="哈哈~我将会被序列化,不管我是是否被transient关键字修饰";

   @Override
    public voidwriteExternal(ObjectOutput out) throws IOException{
      out.writeObject(content);
   }

   @Override
    public voidreadExternal(ObjectInput in) throws IOException,
         ClassNotFoundException {
       content =(String) in.readObject();
   }

    publicstatic void main(String[] args) throws Exception {
      ExternalizableTest et = new ExternalizableTest();
       ObjectOutputout = new ObjectOutputStream(new FileOutputStream(
             newFile("ext0000")));
      out.writeObject(et);

       ObjectInputin = new ObjectInputStream(new FileInputStream(newFile(
            "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

0 0