Java Serialization

What is Serialization?

In Java, object serialization means representing an object as a sequence of bytes. The bytes includes the object’s data and information. A serialized object can be written into a file/database, and read from the file/database and deserialized. The bytes that represent the object and its data can be used to recreate the object in memory.

Why Do We Need Serialization?

Serialization is usually used when you need to send object over network or stored in files. Network infrastructure and hard disk can only understand bits and bytes but not Java objects. Serialization translate Java objects to bytes and send it over network or save it.

Why we want to store or transfer an object? In my programming experience, I have the following reasons that motivate me to use serializable objects.

  1. An object creation depends on a lot of context. Once created, its methods as well as its fields are required for other components.
  2. When an object is created and it contains many fields, we are not sure what to use. So store it to database for later data analysis.

Java Serialization Example

The following example shows how to make a class serializable and serialize & deserialize it.

package serialization;
 
import java.io.Serializable;
 
public class Dog implements Serializable {
	private static final long serialVersionUID = -5742822984616863149L;
 
	private String name;
	private String color;
	private transient int weight;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String getColor() {
		return color;
	}
 
	public void setColor(String color) {
		this.color = color;
	}
 
	public int getWeight() {
		return weight;
	}
 
	public void setWeight(int weight) {
		this.weight = weight;
	}
 
	public void introduce() {
		System.out.println("I have a " + color + " " + name + ".");
	}
}
package serialization;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
public class SerializeDemo {
	public static void main(String[] args) {
		//create an object
		Dog e = new Dog();
		e.setName("bulldog");
		e.setColor("white");
		e.setWeight(5);
 
		//serialize
		try {
			FileOutputStream fileOut = new FileOutputStream("./dog.ser");
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(e);
			out.close();
			fileOut.close();
			System.out.printf("Serialized dog is saved in ./dog.ser");
		} catch (IOException i) {
			i.printStackTrace();
		}
 
		e = null;
 
		//Deserialize
		try {
			FileInputStream fileIn = new FileInputStream("./dog.ser");
			ObjectInputStream in = new ObjectInputStream(fileIn);
			e = (Dog) in.readObject();
			in.close();
			fileIn.close();
		} catch (IOException i) {
			i.printStackTrace();
			return;
		} catch (ClassNotFoundException c) {
			System.out.println("Dog class not found");
			c.printStackTrace();
			return;
		}
 
		System.out.println("\nDeserialized Dog ...");
		System.out.println("Name: " + e.getName());
		System.out.println("Color: " + e.getColor());
		System.out.println("Weight: " + e.getWeight());
 
		e.introduce();
 
	}
}

Output:

Serialized dog is saved in ./dog.ser
Deserialized Dog...
Name: bulldog
Color: white
Weight: 0
I have a white bulldog.

1 thought on “Java Serialization”

Leave a Comment