Reprinted from Http://www.cnblogs.com/liuling/archive/2013/05/05/transient.html
1. The Transient keyword can only modify variables, not methods and classes. Note that local variables cannot be modified by the transient keyword.
2. Variables modified by transient keyword can no longer be serialized, and a static variable can not be serialized, whether or not it is transient modified.
3. Once the variable is transient modified, the variable will no longer be part of the object persistence, and the variable content cannot be accessed after serialization. It can also be assumed that after the persisted object is deserialized, the variable modified by transient will be initialized as normal class member variables.
As the following example
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"));
}
}
The program will output:
0
Date is null
This means that once a variable is transient modified, the variable is no longer part of the object's persistence, and the contents of the variable cannot be accessed after serialization.
Consider the following example:
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 = "haha ~ I will be serialized, regardless of whether I was transient keyword modifier";
@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 (
"ext0000")));
Externalizabletest Et1 = (externalizabletest) in.readobject ();
System.out.println (et1.content);
Out.close ();
In.close ();
}
}
After the program runs, the following results are output:
haha ~ I'll be serialized, no matter if I'm transient keyword modifiers
What is this, not to say that the variables of the class will not be serialized after the Transient keyword is modified?
We know that in Java, the serialization of objects can be implemented by implementing two interfaces, if the operation is a serializable object, then all the serialization will be automatic, if the operation is a Externalizable object, then nothing can be automatically serialized, You need to manually specify the variable you want to serialize in the Writeexternal method, regardless of whether it is transient decorated. So the second example outputs the contents of the variable content initialization, NOT NULL.
This article is from the "Thoughtful Code" blog, so be sure to keep this source http://wujuxiang.blog.51cto.com/2250829/430211
java keyword transient