java关键字tranisent用法详解

作为java中的一个关键字,tranisent用的并不是很多,但是在某些关键场合,却又起着极为重要的作用,因此有必要对它进行一些必要的了解。

一、定义:声明不用序列化的成员域。(源自百度百科)

二、作用:根据tranisent关键字的定义,我们可以很容易的归纳出它的作用,那就是修饰变量,使之不能成为对象持久化的一部分,当然,对于tranisent修饰的这个变量,也是有要求的,这个变量不能是本地变量,如果是用户自定义的变量,那么拥有这个变量的类必须实现Serializable接口;根据之前对static和final的描述,我们知道它们除了可以修饰变量之外,还可以修饰方法和类,但是对于tranisent来说,它有且仅有可以修饰变量的作用,对于方法和类,它是不允许修饰的。

1.修饰变量:对于tranisent修饰的变量,是不允许被序列化的。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Rectangle implements Serializable {
    private static final long serialVersionUID = 1710022455003682613L;
    private Integer width;
    private Integer height;
    private transient Integer area;



    public Rectangle (Integer width, Integer height){
        this.width = width;
        this.height = height;
        this.area = width * height;
    }

    public void setArea(){
        this.area = this.width * this.height;
    }

    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer(40);
        sb.append("width : ");
        sb.append(this.width);
        sb.append("\nheight : ");
        sb.append(this.height);
        sb.append("\narea : ");
        sb.append(this.area);
        return sb.toString();
    }
}

public class TransientExample {
    public static void main(String args[]) throws Exception {
        Rectangle rectangle = new Rectangle(3,4);
        System.out.println("1.原始对象\n"+rectangle);
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("rectangle"));
        // 往流写入对象
        o.writeObject(rectangle);
        o.close();

        // 从流读取对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("rectangle"));
        Rectangle rectangle1 = (Rectangle)in.readObject();
        System.out.println("2.反序列化后的对象\n"+rectangle1);
        rectangle1.setArea();
        System.out.println("3.恢复成原始对象\n"+rectangle1);
        in.close();
    }
}

输出结果:

1.原始对象
width : 3
height : 4
area : 12
2.反序列化后的对象
width : 3
height : 4
area : null
3.恢复成原始对象
width : 3
height : 4
area : 12

注:被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。

那么又会产生一个新的问题,是否被transient关键字修饰的变量真的就不能被序列化呢?

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 Person implements Externalizable {
 
    private transient String content = "是的,我会被序列化";
 
    @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 {
        Person person = new Person();
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
                new File("test")));
        out.writeObject(person);
 
        ObjectInput in = new ObjectInputStream(new FileInputStream(new File(
                "test")));
        person = (Person) in.readObject();
        System.out.println(person.content);
 
        out.close();
        in.close();
    }
}

输出结果:

是的,我会被序列化

十分意外的是,被transient关键字修饰的变量竟然也可以序列化了,这又是为什么呢?原来之前我们所说的,全都是建立在该类是实现Serializable接口的,但是我们知道,在java中,对象的序列化是可以通过两种接口来实现的,一个是Serializable接口,另一个就是上图中所展示的Externalizable接口,至于二者的具体区别,这里不详细诉说了,但是实现Serializable接口,所有的序列化都是自动进行的,实现Externalizable接口则不同,需要我们在writeExternal方法中进行手动序列化,因此这个时候transient就不能够起到什么作用了,这也是为什么在上面的例子中变量content能够被序列化的原因。

总结:虽然transient关键字不是那么常用,但是却不代表它不重要,这里我就简单的小结了一下,如果大家还有什么好的建议,欢迎在评论区留言。

参照:http://www.cnblogs.com/chenpi/

猜你喜欢

转载自www.cnblogs.com/libinhyq/p/9376818.html