Velocity Foundation
Java Notes
Serialization
Velocity Foundation
Serialization-
Serialization is the conversion of the state of an object into a byte stream
called as serialization and Deserialization is the reverse process where the byte
stream is used to recreate the actual Java object in memory.
While serialization a sequence of bytes that includes the object's data as well as
information about the object's type and the types of data stored in the object.
It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.
The serialization process is instance-independent; for example, we can serialize
objects on one platform and deserialize them on another.
Most impressive is that the entire process is JVM independent, meaning an object
can be serialized on one platform and deserialized on an entirely different platform.
Classes that are eligible for serialization need to implement a special
marker interface, Serializable.
How to implement serialization in java
By using the input and output stream, we can do it.
Example-1 Suppose I have one student class in which first name, last name and
mobile number. I just want to store that into file name. Then go for serialization.
package [Link];
import [Link];
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String firstname;
String lastname;
String city;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
[Link] = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
[Link] = lastname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
[Link] = city;
}
package [Link];
import [Link];
import [Link];
public class SerializeStudent {
public static void main(String[] args) {
Student s = new Student();
[Link]("ajay");
[Link]("pawar");
[Link]("pune");
try {
FileOutputStream fos = new FileOutputStream("C:\\Users\\
ThisPC\\Desktop\\[Link]");
ObjectOutputStream oos = new ObjectOutputStream(fos);
[Link](s);
[Link]();
[Link]();
[Link]("Serialization is done...");
} catch (Exception e) {
[Link]();
}
}
}
package [Link];
import [Link];
import [Link];
public class DeserializeStudent {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("C:\\Users\\ThisPC\\
Desktop\\[Link]");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = [Link](); // Read the object
Student s = (Student) o;// convert to student
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
Output:
ajay
pawar
pune
The ObjectOutputStream and ObjectInputStream are used to serialize and de-
serialize objects respectively.
If we don't want to serialize some fields of class then we use the transient
keyword. If any member is declared as transient then it won't be serialized.
If the superclass implements serializable interface, then all its subclasses will
be serializable by default.
All static members of class are not serialized because static members are
related to class only, not to object.
The serialization associated with each serializable class has a version number
called Serial Version UID.
It is used during de-serialization to verify that the sender and receiver of a
serialized object have loaded classes for that and are compatible with respect
to serialization.
If the receiver is loaded with different version of a class that has different
serial version UIDs than the corresponding sender's class, then de-
serialization will result in an invalid Class Exception.
A Serializable class can declare its own serial version UID explicitly by
declaring a field named serial version UID that must be static, final and of
type long.
If a superclass variable is made transient, then after de-serialization, it gives
default value like zero or null.
Example 2-Consider the above same program in which we don't want to serialize
the salary of a student
package [Link];
import [Link];
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String firstname;
String lastname;
transient int salary;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
[Link] = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
[Link] = lastname;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
[Link] = salary;
}
package [Link];
import [Link];
import [Link];
public class SerializeStudent {
public static void main(String[] args) {
Student s = new Student();
[Link]("ajay");
[Link]("pawar");
[Link](5000); //wont be serialized
try {
FileOutputStream fos = new FileOutputStream("C:\\Users\\
ThisPC\\Desktop\\[Link]");
ObjectOutputStream oos = new ObjectOutputStream(fos);
[Link](s);
[Link]();
[Link]();
[Link]("Serialization is done...");
} catch (Exception e) {
[Link]();
}
}
}
package [Link];
import [Link];
import [Link];
public class DeserializeStudent {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("C:\\Users\\ThisPC\\
Desktop\\[Link]");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = [Link](); // Read the object
Student s = (Student) o;// convert to student
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
Output:
ajay
pawar
0