English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Java, serialization is a concept through which we can write the state of an object to a byte stream, so that we can transmit it over the network (using technologies such as JPA and RMI).
Serialize object-
Ensure that the class implements the Serializable interface.
Create aFileOutputStreamObject, which represents the file to which the object will be stored (abstract path).
By passing the above createdFileOutputStreamObject to create an ObjectOutputStream object.
Use the writeObject() method to write an object to a file.
Deserialize object
Create aFileInputStreamAn object that represents a file containing serialized objects.
Use the readObject() method to read an object from a file.
Use the retrieved object.
AConstructor Similar methods and the objects of the class created at the time of calling it are usually used to initialize the variables of an instance of a class. The constructor has the same name as its class and has no return type.
If a constructor is not provided, the compiler will represent you define a constructor that will initialize instance variables with default values.
When we deserialize an object, we never call its class constructor. Consider the following example, where there is a class named Student with two instance variables and a default constructor (initialized with two hardcoded values) and a parameterized constructor.
The display() method of this class displays the variable values of the current instance.
We pass two values (vani and27) and serialize it to create an object of the Student class.
When we deserialize this object and call the display() method, the values we pass will be printed. Static variables will print the new (current) value in the class.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Student implements Serializable{ private String name; private transient int age; private static int year = 2018; public Student(){ System.out.println("This is a constructor"); this.name = "Krishna"; this.age = 25; } public Student(String name, int age){ this.name = name; this.age = age; } public void display() { System.out.println("Name: ",+this.name); System.out.println("Age: ",+this.age); System.out.println("Year: ",+Student.year); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setYear(int year) { Student.year = year; } } public class SerializeExample{ public static void main(String args[]) throws Exception{ //Create Student object Student std = new Student("Vani", 27); //Serialize object FileOutputStream fos = new FileOutputStream("e:\\student.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(std); oos.close(); fos.close(); //Print data before deserialization System.out.println("Value before deserialization"); std.display(); //Change static variable value std.setYear(2019); //Change instance variable value std.setName("Varada"); //Change transient variable value std.setAge(19); System.out.println("Object serialized......"); //Deserialize object FileInputStream fis = new FileInputStream("e:\\student.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Student deSerializedStd = (Student) ois.readObject(); System.out.println("Object de-serialized......"); ois.close(); fis.close(); System.out.println("Value after deserialization"); deSerializedStd.display(); } }
Output result
Value before deserialization Name: Vani Age: 27 Year: 2018 Object serialized...... Object de-serialized...... Value after deserialization Name: Vani Age: 0 Year: 2019