This content has been marked as final. Show 5 replies
Update - actually I can use the "valueOf(String name)" method that is automatically generated for the Enum class.
From the javadocs
Enum constants are deserialized 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 transmitted. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the static method Enum.valueOf(Class, String) with the enum constant's base type and 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 deserialized cannot be customized: any class-specific readObject, readObjectNoData, and readResolve methods defined by enum types are ignored during deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L.
java enums are Serializable. you don't need to do anything special, just "out.writeObject(enum)" and "enum = in.readObject()".
note, just because the top-level object is Externalizable does not mean all nested references must also be Externalizable.
Edited by: jtahlborn on Dec 15, 2010 1:40 PM
Yes, I don't have to do anything special. My concern is that changes in the next release may break something. So I will write out the Enum object as its string value instead. Then when I read it in, I can use the valueOf(String) method to get the Enum object. If something changes, I can add special handling for the string values.
My concern is that changes in the next release may break something.The Java language Specification prevents that in the case of Enum.
So I will write out the Enum object as its string value instead.That's exactly what java.lang.Enum#writeObject() already does.
Then when I read it in, I can use the valueOf(String) method to get the Enum object.That's exactly what java.lang.Enum#readObject() already does.
You don't need to worry about this changing.