In this tutorial, we will see serialization and deserialization of Enums and sample java codes.

Enums Serialization:

  • Java’s Serializable interface handles serialization of enums in a special way.
  • Enum types are special types in Java that are a kind of classes. They are similar to classes but have some restrictions, one of them is that they are final and can’t be subclassed. Because of this, enum types are handled differently during serialization.
  • When an object that contains an enum type field is serialized, the ObjectOutputStream writes the enum type’s name and the enum constant’s name to the output stream. When the object is deserialized, the ObjectInputStream reads the enum type’s name and the enum constant’s name from the input stream and uses them to reconstruct the enum constant.
  • This mechanism ensures that the enum type and the enum constant are correctly reconstructed during deserialization, even if the enum type has been modified or the enum constant has been removed or added.
  • It’s important to note that if the enum type is modified between the time the object is serialized and deserialized, the deserialization process will fail with an InvalidClassException.
  • In summary, serialization in Java handles enum type fields by writing the enum type’s name and the enum constant’s name to the output stream during serialization, and reading the enum type’s name and the enum constant’s name from the input stream during deserialization. This mechanism ensures that the enum type and the enum constant are correctly reconstructed during deserialization even if the enum type has been modified.

Enum serialization in Java Code:

import java.io.Serializable;
//Jayaram: Javasavvy
public class EnumExampleDemo implements Serializable {
    private static final long serialVersionUID = 1L;
    private EnumColor color;

    public MyClass(EnumColor myEnum) {
        this.color= myEnum;
    }

    public EnumColor getEnumColorField() {
        return color;
    }

    public void setEnumColor(EnumColor myEnumField) {
        this.color= myEnumField;
    }

    public static enum EnumColor{
        RED, GREEN
    }
}

In the above examples, EnumExampleDemo class that implements Serializable interface and contains an enum type field color of EnumColor which has two enum constants, RED and GREEN

We need to use ObjectOutputStream and ObjectInputStream classes to serialize and deserialize an instance of EnumExampleDemo:

EnumExampleDemo obj = new EnumExampleDemo(EnumExampleDemo.EnumColor.RED);

// Serialize the object
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("EnumExampleDemp.ser"))) {
    out.writeObject(obj);
}

// Deserialize the object
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("EnumExampleDemo.ser"))) {
    EnumExampleDemo deserializedObj = (EnumExampleDemp) in.readObject();
    System.out.println(deserializedObj.getEnumColorField);
}

It’s important to note that the enum type must be available during deserialization, so you should not remove any enum constant or change the order of enum constants between serialization and deserialization.

Thanks for Reading…