Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use constant types for safer and cleaner code

Avoid making typos while encapsulating arbitrary data in legacy systems in an elegant object-oriented way

  • Print
  • Feedback
A couple readers pointed out that one of the constructors for the SmartIntArray has a bug in it. This constructor, which takes only the initial size of the array, calculates the growth size by dividing that initial size by four. If the initial size provided is less than four, the growth size will be calculated as zero, and thus an ArrayOutOfBoundsException will be thrown as soon as the initial size is exceeded. One way to avoid this is to check for a growth size of zero and reset it to one. Another alternative is to define a minimum growth size. I prefer the latter because, the smaller the growth size is, the less efficient the tool becomes.

A few of you also commented that the Vector class has a method called copyInto() which places the vector's contents into the provided array of type Object. This is of no help for primitive types, which is why I wrote the smart arrays. However, the SmartStringArray version is unnecessary, since String is an object, not a primitive, and thus can be passed into the vector's utility method.

On to the new tool

This month's tool is a variation on a theme. I will be expanding on the idea of enumerated constants as covered in Eric Armstrong's July 1997 JavaWorld article, "Create enumerated constants in Java." I strongly recommend reading that article before you immerse yourself in this one, as I will assume that you are familiar with the concepts related to enumerated constants, and I will expand on some of the example code which Eric presented.

As a note before we get started, some of you have commented that, in the tool implementations outlined in my recent articles, I have not taken advantage of the new Java 2 features. This is intentional. In my line of work, I don't see many companies diving into Java 2 yet. Thus, I am presenting tools that are compatible with, and expand on, the 1.1 version of Java.

The concept of constants

In dealing with enumerated constants, I'm going to discuss the enumerated part of the concept at the end of the article. For now, we'll just focus on the constant aspect. Constants are basically variables whose value can't change. In C/C++, the keyword const is used to declare these constant variables. In Java, you use the keyword final. However, the tool introduced here is not simply a primitive variable; it's an actual object instance. The object instances are immutable and unchangeable -- their internal state may not be modified. This is similar to the singleton pattern, where a class can may only have one single instance; in this case, however, a class may only have a limited and predefined set of instances.

The main reasons to use constants are clarity and safety. For example, the following piece of code is not self-explanatory:

    public void setColor( int x ){ ... }
    
    public void someMethod()
    {
        setColor( 5 );
    }


From this code, we can ascertain that a color is being set. But what color does 5 represent? If this code were written by one of those rare programmers who comments on his or her work, we might find the answer at the top of the file. But more likely we'll have to dig around for some old design documents (if they even exist) for an explanation.

  • Print
  • Feedback

Resources