• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Serialize/Deserialize Java enum

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After two days I finally figured out why (i think) my enum class is not getting deserialzed properly.


Now lets say that I programatically set the length and width and serialize to a file.


If I close my application and try to deserialize, or deserialize from a different application I lose the length and width settings.

My question is this: Is there a way that I can write custom serialize/deserialze code to prevent this behavioir? I.E. override writeObject and readObject? If I can how would I do that?

Thanks in advance!
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this dosen't sound too silly, but shouldn't your enum implement Serializable?
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not according to your quote.
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. readObject is also not possible since java.lang.Enum implements that to throw an exception. Any attempt to implement this method yourself will fail.

To be honest, I think your design is flawed. Modifiable enums? Really?
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

You are correct my design is flawed.

I think what I really wanted after thinking about it is something like this (which works nicely)
 
Rob Spoor
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd make the inner enum public, or it will only be visible from the same package. Unless that's what you want of course.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
et al

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 ?? "

Regards,
TK
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Theodore David Williams wrote:Rob,

You are correct my design is flawed.

I think what I really wanted after thinking about it is something like this (which works nicely)



What you really want is a class with a private constructor and three static instances of that class, ONE, TWO, or THREE

Then serialize those, and reset them during deserialization. No need to cram Enum into something it was never meant to do or be.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic