Deserializing enum in Java

Viewed 326

I can see abstract class enum has implementation of readObject() method of marker interface Serializable which throws InvalidObjectException.

private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
        throw new InvalidObjectException("can't deserialize enum");
}

private void readObjectNoData() throws ObjectStreamException {
        throw new InvalidObjectException("can't deserialize enum");
}

My understanding from this is when deserializing an enum we can get InvalidObjectException. But I am able to deserialize my enum.

As per my understanding if readResolve() method implementation is provided then jvm calls this method during deserializing so technically i should get an error but instead I am getting object.

my code:

enum Person50{
    OBJECT;
}
private static void serializationSingletonEnum() throws Exception{
        Person50 p50 = Person50.OBJECT;
        System.out.println("hashCode 1:" +p50.hashCode());

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("enumFile.ser"));
        oos.writeObject(p50);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("enumFile.ser"));
        Person50 p15 = (Person50)ois.readObject();
        System.out.println("hashCode after:" +p15.hashCode());
}

Output:

hashCode 1:1435804085
hashCode after:1435804085

I might be committing some silly mistake I think so or my understanding about serialization invocation might be incorrect.

1 Answers

The reason it works is that the Enum serialization process ignores readObject() implementation

As per official documentaion

1.12 Serialization of Enum Constants Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not present in the form. To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant's name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java.lang.Enum.valueOf method, passing the constant's enum type along with the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream.

The process by which enum constants are serialized cannot be customized: any class-specific writeObject, readObject, readObjectNoData, writeReplace, and readResolve methods defined by enum types are ignored during serialization and deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L. Documenting serializable fields and data for enum types is unnecessary, since there is no variation in the type of data sent.

Related