This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hope this dosen't sound too silly, but shouldn't your enum implement Serializable?
SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Theodore David Williams
Joined: Dec 21, 2009
It can, but java enums already implement Serializable not no need to re-implement.
From the Java Serialization Spec:
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 transmitted. To serialize an enum constant, ObjectOutputStream writes the string returned by the constant's name method. 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 and writeReplace methods defined by enum types are ignored during serialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L.
Is there any way to achieve the behavior I want???
I agree with moving the setter out of Enums, as enums are supposed to be constants. You should not change them once they are created.
As far as serialization is concerned, you should be careful how you serialize the enum value to a database or disk. You should not use the cardinal value or the constant literal to serialize. Use a business value, which is unlikely to change. e.g. "one", "two" in the above example. If interested see my article 3 ways to serialize Java Enums
Enums are automatically serializable and comparable.
here are couple of suggestions that you might want to try out
1. initialize the length and width in the constructor rather than setting them afterwards
2. make the enum public and possibly put it in a different .java file?? -> Good Practice. saves lots valuable time in human lives while figuring out " ?? why ?? "