Object Serialization in Java | Generated by AI

Home PDF

I assume you meant “java.io.Serializable” - a key interface in Java used for object serialization. Here’s a practical guide on how to use it:

What is Serializable?

java.io.Serializable is a marker interface (no methods to implement) that tells the Java runtime that a class can be serialized - converted into a byte stream to be saved to a file, sent over a network, or stored in a database, and later deserialized back into an object.

Basic Steps to Use Serializable

  1. Implement the Interface Add implements Serializable to your class. That’s the minimum requirement.

    import java.io.Serializable;
    
    public class Person implements Serializable {
        private String name;
        private int age;
    
        // Constructor
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        // Getters and setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public int getAge() { return age; }
        public void setAge(int age) { this.age = age; }
    
        @Override
        public String toString() {
            return "Person{name='" + name + "', age=" + age + "}";
        }
    }
    
  2. Serialization - Writing the Object Use ObjectOutputStream to write the object to a file or stream.

    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.IOException;
    
    public class SerializeExample {
        public static void main(String[] args) {
            Person person = new Person("Alice", 25);
    
            try (FileOutputStream fileOut = new FileOutputStream("person.ser");
                 ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
                out.writeObject(person);
                System.out.println("Serialized data is saved in person.ser");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
  3. Deserialization - Reading the Object Use ObjectInputStream to read the object back.

    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;
    
    public class DeserializeExample {
        public static void main(String[] args) {
            try (FileInputStream fileIn = new FileInputStream("person.ser");
                 ObjectInputStream in = new ObjectInputStream(fileIn)) {
                Person person = (Person) in.readObject();
                System.out.println("Deserialized Person: " + person);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    

Key Points to Remember

Example with transient and serialVersionUID

import java.io.Serializable;

public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private transient String password; // Won't be serialized
    private int id;

    public Employee(String name, String password, int id) {
        this.name = name;
        this.password = password;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{name='" + name + "', password='" + password + "', id=" + id + "}";
    }
}

Testing It

Serialize and deserialize Employee. You’ll notice password is null after deserialization because it’s transient.

Common Use Cases

Pitfalls

Custom Serialization (Optional)

If you need control over serialization, implement these methods:

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    // Custom logic
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    // Custom logic
}

Let me know if you want a deeper dive into any part!


Back 2025.03.04 Donate