2008年7月4日金曜日

[JAVA] transient field

transient Field

シリアライズする時保存されなくなる。
직렬화후에 보존되지 않는 필드가 됨.


// TEST SAMPLE
package test;

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

public class TransientTest implements Serializable {
private static final long serialVersionUID = 1L;

private transient String a;

private String b;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

/**
* @param args
*/
public static void main(String[] args) throws Exception {
TransientTest test = new TransientTest();
test.setA("aaa");
test.setB("bbb");
System.out.println("A=" + test.getA());
System.out.println("B=" + test.getB());

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
"test.tmp"));
out.writeObject(test);
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"test.tmp"));
TransientTest clone = (TransientTest) in.readObject();
System.out.println("A=" + clone.getA());
System.out.println("B=" + clone.getB());
}
}

// RESULT
A=aaa
B=bbb
A=null
B=bbb