ObjectOutputStream Class in Java

In this article, we will learn how to use ObjectOutputStream Class in Java with an example.

1. Introduction to ObjectOutputStream Class

An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream.

Only objects that support the java.io.Serializable interface can be written to streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.
The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject

Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written.

2. ObjectOutputStream class Constructors

  • protected ObjectOutputStream() - Provide a way for subclasses that are completely reimplementing ObjectOutputStream to not have to allocate private data just used by this implementation of ObjectOutputStream.
  • ObjectOutputStream(OutputStream out) - Creates an ObjectOutputStream that writes to the specified OutputStream.

3. ObjectOutputStream class Methods

This class diagram shows a list of methods ObjectOutputStream class provides in Java.

4. ObjectOutputStream class Example

In this ObjectOutputStreamExample, we are going to serialize the object of the Student class. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the object in the file named student.dat.

1. Define Student Class

Let's create a class Student which implements the Serializable interface:
import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    private String name;
    private int rollNo;
    private double gradePoint;

    public Student(String name, int rollNo, double gradePoint) {
        this.name = name;
        this.rollNo = rollNo;
        this.gradePoint = gradePoint;

    public String toString() {
        return "Student{name='" + name + "', rollNo=" + rollNo + ", gradePoint=" + gradePoint + '}';

2. Serialization using ObjectOutputStream 

Let's serialize a Student object and save it to a file named "student.dat".
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

public class SerializeStudent {

    public static void main(String[] args) {
        // File name
        String filename = "student.dat";

        // Create a new student object
        Student student = new Student("John", 101, 3.9);

        // Serialize the Student object
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {

            System.out.println("Student object serialized successfully to " + filename);

        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());

// Expected Output:
// Student object serialized successfully to student.dat
This example uses the try-with-resources statement to close the resources automatically.

This example initializes a Student object and serializes it to the "student.dat" file. The object can then be deserialized using ObjectInputStream as demonstrated in the previous answer.

5. Advantages of ObjectOutputStream 

Flexibility: Allows you to serialize complex object graphs with ease. 

Integration with Java's Type System: Automatically handles different object types, arrays, and even Enums. 

6. Considerations 

Versioning: If the class changes (fields are added or removed) after serialization, deserialization might break. Use the serialVersionUID field to manage versions. 

Security: Be cautious when deserializing objects from untrusted sources, as they may execute malicious code. 

Transient Fields: Fields marked as transient won't be serialized. This is useful for fields that should not or cannot be saved.