5 Replies Latest reply: Dec 22, 2010 4:12 PM by EJP RSS

    Externalizable Enum

    824227
      Hi,

      I need to make a class Externalizable. One of its member variables is an Enum. It looks like I cannot make the Enum Externalizable because it does not have a default constructor. What is my best alternative? I think I should write out the "name" value from the Enum instance. Then when I read it back in I can reconstruct the Enum instance using the valueOf(Class, name) method since I know the class and I will get the name. Right?

      Thanks.
        • 1. Re: Externalizable Enum
          824227
          Update - actually I can use the "valueOf(String name)" method that is automatically generated for the Enum class.
          • 2. Re: Externalizable Enum
            824227
            From the javadocs
            http://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html

            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.
            • 3. Re: Externalizable Enum
              jtahlborn
              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
              • 4. Re: Externalizable Enum
                824227
                Hi,

                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.

                Thanks.
                • 5. Re: Externalizable Enum
                  EJP
                  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.