Transient keyword – Transient
keyword is applicable only for variables. At the time of serialization if we
don’t want to save the original value of a particular variable to meet the
security constraints, such type of variables should be declared with transient
keyword. At the time of serialization jvm ignores the original value of
transient variable and saves default values. Hence transient means not to
serialize.
Transient v/s Static – static variable
aren’t part of object state. Hence they don’t participate in serialization.
Declaring static variable as transient has no impact. Example – transient static
int i=10; Output - i=10.
But
transient int i=10; Output – i=0;
Transient v/s final – Final variables
will participate in serialization directly by their values. Hence declaring the
final variable as transient has no impact (because every final variable is
replaced by value at the time of compilation only). Example – transient final
int j=10; Output – j=10;
Customized Serialization – During default
Serialization there may be a chance of loss of information due to transient
keyword.
Example – To implement
loss of object data into file.
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
class Example
implements java.io.Serializable{
private
String uname;
private transient
String upwd;
public
String getUname() {
return uname;
}
public void
setUname(String uname) {
this.uname
= uname;
}
public
String getUpwd() {
return upwd;
}
public void
setUpwd(String upwd) {
this.upwd
= upwd;
}
}
public class
TransientImple {
public static void
main(String[] args) throws FileNotFoundException, IOException,
ClassNotFoundException {
Example e1 = new
Example();
e1.setUname("rakesh");
e1.setUpwd("pw1234");
ObjectOutputStream
oos = new ObjectOutputStream(new
FileOutputStream("xyz.txt"));
oos.writeObject(e1);
ObjectInputStream
ois = new ObjectInputStream(new FileInputStream("xyz.txt"));
Example e2 = (Example)ois.readObject();
System.out.println(e2.getUname());
System.out.println(e2.getUpwd());
}
}
Output –
rakesh
null
Explanation – In the
above program before performing serialization Example object can provide proper
username and password but after Deserialization Example object can provide only
username but not password. This is due to transient keyword. Hence during
default serialization there may be a chance of loss of information due to
transient keyword. We can recover this loss of information by using atomized
serialization. We can implement customized serialization by using the following
two methods –
1. private void
writeObject(ObjectOutputStream os) throws Exception – This method
will be executed automatically at the time of serialization, if we want to
perform any extra work we have to define that inside this method only.
2. private void
readObject(ObjectInputStream is) throws Exception – This method
will be executed automatically at the time of Deserialization. Hence, at the
time of Deserialization we want to perform any extra work. We have to define
that inside method only.
Example – To implement
loosing the value of object and recovering that object and to store in a file.
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
class Example1
implements java.io.Serializable{
private
String uname;
private transient
String upwd;
public
String getUname() {
return uname;
}
public void
setUname(String uname) {
this.uname
= uname;
}
public
String getUpwd() {
return upwd;
}
public void
setUpwd(String upwd) {
this.upwd
= upwd;
}
private void
writeObject(ObjectOutputStream os)throws Exception
{
os.defaultWriteObject();
String epass="e123"+upwd;
os.writeObject(epass);
}
private void
readObject(ObjectInputStream is)throws Exception
{
is.defaultReadObject();
String epass=(String)is.readObject();
upwd=epass.substring(4);
}
}
public class
Trans2 {
public static void
main(String[] args) throws FileNotFoundException, IOException,
ClassNotFoundException {
Example1 e1 = new
Example1();
e1.setUname("rakesh");
e1.setUpwd("pw1234");
ObjectOutputStream
oos = new ObjectOutputStream(new
FileOutputStream("lmn.txt"));
oos.writeObject(e1);
ObjectInputStream
ois = new ObjectInputStream(new FileInputStream("lmn.txt"));
Example1 e2 = (Example1)ois.readObject();
System.out.println(e2.getUname());
System.out.println(e2.getUpwd());
}
}
Output –
rakesh
pw1234
Explanation – In the
above example Example1 class the writeObject() and readObject() methods will be
executed automatically by the jvm. Hence these are callback methods.
No comments:
Post a Comment