What is transient?
transient is a keyword in java, to keep object from serializing. Once modified by transient, you can not visit the object after serializing.
transient can only be used to modify variables, but methods and classes. And what calls for particular note is that local variables can not be modified by it.
With reference to it, static variables can never be serialized. Because static variables remain in memory forever. where you create, where it stays.
But if you implements Externalizable interface, there is nothing with transient.
What is Externalizable?
Externaliazable extends from Serializable, can serialize objects selectively. Only when you overwrite writeExternal(ObejectOutput out) (readExternal(ObjectInput in), relatively), can you point out what can be serialized (deserialized, relatively). But as to Serializable interface, the default is to serialize all objects except modified by transient.
To serialize, should only implements Serializable interface.
public class User implements Serializable{
/** name can be serialized. */
public String name;
/** password won't be serialized. Can not visit it, either.*/
public transient String password;
/** setter && getter */
/*pass*/
}
To Test Serializable and transient.
/** For test. */
public class Test {
public static void main(String[] args){
User user = new User();
user.setName("name");
user.setPassword("password");
/**There are 2 classes for being serializable: ObjectOutputStream, ObjectInputStream;
* ObjectOutputStream: to write object into byte stream; method: writeObject() to serialize objects recursively;
* ObjectInputStream: to rebuild original object from byte stream; method: readObject();
* */
try{
FileOutputStream fos = new FileOutputStream("D:/test.txt");
ObjectOutputStream oos = new OvbjectOutputStream(fos);
oos.writeObject(user); //if more than one objects, invoke the matching writeXXX();
oos.flush();
oos.close();
}catch(Exception e){
}
/** to rebuild orginal objects from byte streams. */
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/test.txt"));
User user0 = (User) ois.readObject();
ois.close();
}catch(Exception e){
}
}
}
To Externalizable.
/**
* Created by ibyr on 2016/9/12.
*/
public class User implements Externalizable {
private String name;
private String password;
private int age;
public User(){
}
public User(String name, String password, int age){
this.name = name;
this.password = password;
this.age = age;
}
/** getter && setter */
/** skip... */
/**
* When serializing object, invoked auto.
* @param out
* @throws IOException
*/
public void writeExternal(ObjectOutput out) throws IOException {
/** we can decide what to be serializable. */
Date date = new Date();
out.writeObject(date);
out.writeObject(name);
out.writeObject(password);
}
/**
* When deserializing, invoked auto.
* @param in
* @throws IOException
* @throws ClassNotFoundException
*/
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
Date date = (Date) in.readObject();
this.name = (String) in.readObject();
this.password = (String) in.readObject();
}
public String toString(){
return "user: " + this.name + "; password: " + this.password + "; age: " + this.age;
}
}