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.