Say we want to save state of some object before modifying the object state. How we can save the state or in simple term value of its variables/fields?
One way is to use file and write the data into the file. For that we need our own logic. We need to follow strict ordering of reads and writes for correct manipulation of the object states[Employee in below example]. For an object which is having a huge number of variables, it becomes a tedious job to implement.
Java provides another easy way to achieve that which is called serialization. java.io.Serializable is a marker interface using which we can serialize/de-serialize one object. If the class of object implements Serializable interface then each and every values except transient will be serialized.
We might need some variables not to be serialized mainly because of security reasons. How we can prevent some variable in the Serializable Employee object to be serialized/de-serialized? Java provides transient keyword to mark one variable of an object non-serializable.
Lets take an example to understand the above concept better:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationTest {
public static void main(String[] args) {
Employee e1=new Employee("James","Bond", "007");
try {
ObjectOutputStream oout = new ObjectOutputStream(
new FileOutputStream("filename"));
oout.writeObject(e1);
oout.flush();
oout.close();
ObjectInputStream oin = new ObjectInputStream(
new FileInputStream("filename"));
Object o1 = oin.readObject();
System.out.println(o1);
} catch (Exception e){
e.printStackTrace();
}
}
}
class Employee implements Serializable{
String firstName;
String lastName;
String empId;
transient String securityKey;
public Employee(String fname,String lname,String id) {
this.firstName=fname;
this.lastName=lname;
this.empId=id;
this.securityKey="xyz";
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", firstName=" + firstName
+ ", lastName=" + lastName + ", securityKey=" + securityKey
+ "]";
}
}
Output:
Employee [empId=007, firstName=James, lastName=Bond, securityKey=null]
Note here, since securityKey is marked as transient it got initialized to its default value null after deserialization.
One way is to use file and write the data into the file. For that we need our own logic. We need to follow strict ordering of reads and writes for correct manipulation of the object states[Employee in below example]. For an object which is having a huge number of variables, it becomes a tedious job to implement.
Java provides another easy way to achieve that which is called serialization. java.io.Serializable is a marker interface using which we can serialize/de-serialize one object. If the class of object implements Serializable interface then each and every values except transient will be serialized.
We might need some variables not to be serialized mainly because of security reasons. How we can prevent some variable in the Serializable Employee object to be serialized/de-serialized? Java provides transient keyword to mark one variable of an object non-serializable.
Lets take an example to understand the above concept better:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationTest {
public static void main(String[] args) {
Employee e1=new Employee("James","Bond", "007");
try {
ObjectOutputStream oout = new ObjectOutputStream(
new FileOutputStream("filename"));
oout.writeObject(e1);
oout.flush();
oout.close();
ObjectInputStream oin = new ObjectInputStream(
new FileInputStream("filename"));
Object o1 = oin.readObject();
System.out.println(o1);
} catch (Exception e){
e.printStackTrace();
}
}
}
class Employee implements Serializable{
String firstName;
String lastName;
String empId;
transient String securityKey;
public Employee(String fname,String lname,String id) {
this.firstName=fname;
this.lastName=lname;
this.empId=id;
this.securityKey="xyz";
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", firstName=" + firstName
+ ", lastName=" + lastName + ", securityKey=" + securityKey
+ "]";
}
}
Output:
Employee [empId=007, firstName=James, lastName=Bond, securityKey=null]
Note here, since securityKey is marked as transient it got initialized to its default value null after deserialization.
No comments:
Post a Comment