http://www.javabeat.net/tips/168-what-is-transient-keyword-in-java.html
Author : SuthaDate : Wed Feb 18th, 2009Topic : java
Translated by: iamjemy@gmail.com
什么是序列化?
If you want to understand what is transient, then first learn what is serilization concept in Java if you are not familiar with that. Serilization is the process of making the object's state persistent. That means the state of the object is converted into stream of bytes and stored in a file. In the same way we can use the de-serilization concept to bring back the object's state from bytes. This is one of the important concept in Java programming because thisserilization is mostly used in the networking programming. The object's which are needs to be transmitted throughnetwork has to be converted as bytes, for that purpose ever class or interface must implements Serilizationinterface. It is a marker interface without any methods.
如果你想知道transient是什么,那么你必须首先了解什么是序列化。序列化是对象状态持久化的过程。这意味着对象的状态被转化为字节流并且保存在文件中。同样我们用反序列化的概念来从字节流中恢复对象。这是java编程中的重要概念因为序列化大量运用于网络编程中。需要通过网络传输的对象必需要进行序列化转换为字节流之后才能传输。因此所对应的类和接口必须实现Serilization接口。这个接口没有任何方法,只是一个标记接口。
什么是瞬态?
By default all the variables in the object is converted into the persistent. In some cases, you may want to avoid persisting some variables because you don't have the necesscity to persist those varibale. So, you can declare those variables as transient. if the variable is declared as transient, then it will not be persisted. It is the main purpose of the transient keyword.
默认情况下,对象的所有成员变量都将被持久化。在某些情况下,如果你想避免持久化对象的一些成员变量,你可以使用transient关键字来标记他们。如果一个成员变量被标记为transient,那么它不会被持久化。这是transient关键字的主要用途。
Look into the following example to understand the purpose of transient keyword:
package javabeat.samples; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class NameStore implements Serializable{ private String firstName; private transient String middleName; private String lastName; public NameStore (String fName, String mName, String lName){ this.firstName = fName; this.middleName = mName; this.lastName = lName; } public String toString(){ StringBuffer sb = new StringBuffer(40); sb.append("First Name : "); sb.append(this.firstName); sb.append("Middle Name : "); sb.append(this.middleName); sb.append("Last Name : "); sb.append(this.lastName); return sb.toString(); } } public class TransientExample{ public static void main(String args[]) throws Exception { NameStore nameStore = new NameStore("Steve","Middle","Jobs"); ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream( "nameStore")); // writing to object o.writeObject(nameStore); o.close(); // reading from object ObjectInputStream in =new ObjectInputStream( new FileInputStream("nameStore")); NameStore nameStore1 = (NameStore)in.readObject(); System.out.println(nameStore1); } } // output will be : First Name : SteveMiddle Name : nullLast Name : JobsIn the above example, the variable middleName is declared as transient, so it will not be stored in the persistent storage. You can run the above example and check the results. If you have any doubts please click on the feedback button and send your queries. We will reply back ASAP.