create a Java class Serializable
To make a Java class serializable, you need to implement the java.io.Serializable interface. This is a marker interface, meaning it does not have any methods but indicates to the JVM that objects of this class can be serialized.
Table of Contents
Steps to Make a Class Serializable
1. Implement the Serializable Interface: Add implements to your class definition.
2. Define serialVersionUID (Optional but Recommended): This is a unique identifier for each class that helps in version control during deserialization. If not defined, the JVM generates one automatically.
3. Mark Non-Serializable Fields (Optional): Use the transient keyword for fields that you do not want to serialize.
Advantages of Serialization
1. Persistence: Save the state of an object to a file or database.
2. Communication: Send objects over a network.
3. Caching: Store objects temporarily to improve performance.
4. Deep Copy: Create a deep copy of an object by serializing and deserializing it.
Example
Let’s see how to make a Java class serializable with an example.
java
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L; // Ensures compatibility during deserialization
private String name;
private int age;
private transient String password; // This field will not be serialized
public Employee(String name, int age, String password) {
this.name = name;
this.age = age;
this.password = password;
}
@Override
public String toString() {
return "Employee{name='" + name + "', age=" + age + ", password='" + password + "'}";
}
}
java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeEmployee {
public static void main(String[] args) {
Employee employee = new Employee("Alice", 28, "password123");
try (FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(employee);
System.out.println("Serialized data is saved in employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeEmployee {
public static void main(String[] args) {
Employee employee = null;
try (FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
employee = (Employee) in.readObject();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
System.out.println("Deserialized Employee:");
System.out.println(employee);
}
}
Explanation of the Example
Serializable Class
- The Employee class implements the interface.
- The serialVersionUID ensures version control during serialization and deserialization.
- The transient keyword is used for the password field, which means this field will not be serialized.
Serialization
- The SerializeEmployee class creates an Employee object and serializes it to a file named employee.ser.
- ObjectOutputStream is used to write the Employee object to the file.
Deserialization
- The DeserializeEmployee class reads the Employee object from the employee.ser file using ObjectInputStream.
- The deserialized object is then printed to the console.