Women in Technology

Hear us Roar



Article:
  Seven Low-Cost Ways to Improve Legacy Code
Subject:   Enums and "Constant Object"
Date:   2004-10-28 07:39:21
From:   dubwai
Normally what you call a "Constant Object" is called 'type-safe enum'. I don't really care for the way you use the term constant in relation to Java as there are no constants in Java. There are only finals. Calling them constants is confusing.


But that's not really what this post is about. While I agree with the use of enums if you have the ability to use 1.5, the example of a "Constant Object" could cause problems for an unsuspecting developer. If any of the instances of the Rainbow class you give are serialized and then deserialized, they will create new instances of the class that are not referenced by the class members.


If you are going to provide this example as a way to avoid bugs you need to mention that a good bit of extra work is required to make it serialize and deserialize properly. The example you give will likely introduce new bugs if used with serialization.

Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • Enums and "Constant Object"
    2004-10-28 08:19:42  Robert Simmons, Jr. | O'Reilly Author [View]

    I address this fact in the book in the section on constant objects. Avoiding such a problem is a rather minor task with the readResolve() method. As for the term for it, that is personal preference. The improtant part is that the concept gets through. A

    s for the term "type-safe-enum" I personally think that would cause confusion since the code is not exactly declaring an enum (which has other semantics as well) but merely several objects that cannot be changed (IE constant). There is no convention that says that only integral and string and other primitive types are the only kinds of things that are constant.

    A constant is simply something that can not be changed; the antithesis of variable. As for the term "type-safe-enum pattern, I never read Blochs book until after I wrote my book. Its nto terribly suprising that more than one developer thought of it. Its a rather good idea as Bloch has said himself, no matter what you call it.