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 like JPA and RMI).
Serialize object-
Ensure that this class implements the Serializable interface.
Create aFileOutputStreamAn object representing the file where the object is to be stored (abstract path).
By passing the above createdFileOutputStreamObject to create an ObjectOutputStream object.
Write the object to the file using the writeObject() method.
Deserialize object
Create aFileInputStreamAn object representing a file containing serialized objects.
Read an object from a file using the readObject() method.
Use the retrieved object.
When only deserializing an object, instance variables will be preserved, and they will have the same values after the process.
Transient variables - Never considerTransient variablesof the value (excluding it from the serialization process). For example, when we declare a transient variable, after deserialization, its value will always be null, false, or zero (default value).
Static variables - will not be retained in the deserialization processStatic variablesof the value. In fact, static variables will not be serialized either, but because they belong to the class. After deserialization, they will take the current value from the class.
In this program, we have changed the values of the instance, static, and transient variables of the class before deserialization.
After this process, the values of the instance variables will be the same. Temporary variables will display default values, while static variables will display the new (current) values 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 a 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("Deserialized value:"); deSerializedStd.display(); } }
Output result
Value before deserialization: Name: Vani Age: 27 Year: 2018 Object serialized...... Object de-serialized...... Deserialized value: Name: Vani Age: 0 Year: 2019