another enum gotcha

T

Tony Morris

// Usually you list enum constants in alphabetical order.
// Eclipse will put them in alphabetical order when you do
// a Source Sort Members.

A potential gotcha here is that changing the order of appearance of enum
fields may break existing clients.
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
"Note that each enum type has a static values method that returns an array
containing all of the values of the enum type in the order they are
declared."

Reflection APIs make no such guarantee of order, and so this does not apply
to other type fields.

--
Tony Morris
http://tmorris.net/

Java Questions and Answers
http://jqa.tmorris.net/
 
R

Roedy Green

/**
* example use of Enum, with method implemented a different way for
each enum
* constant.
*/
package com.mindprod.example;

/**
* @author Roedy Green
*/
public enum BreedI {

// Usually you list enum constants in alphabetical order.
// Eclipse will put them in alphabetical order when you do
// a Source Sort Members.

/** Dachshund smooth or curly */
DACHSHUND( "brown" ),

/** Dalmatian */
DALMATIAN( "spotted" ) {

// begin DALMATIAN inner class
/**
* typical spots on a Dalmatian
*/
private final int spots = 50;

/**
* Method getSpotcount is Defined only for DALMATIAN. Get typical
count of
* spots on a Dalmatian
*
* @return spot count
*/
int getSpotCount ()
{
return spots;
}

// end DALMATIAN inner class
},

/** Labrador all sizes */
LABRADOR( "black or tan" );

// end of enum constant constructions

/** additional instance field of every BreedI enum constant object
*/
private String colours;

/**
* additional method of every BreedI enum constant object
*
* @return typical colours of the breed
*/
public String getColours ()
{
return colours;
}

/**
* constructor
*
* @param colours
* typical colours of this breed
*/
BreedI( String colours )
{
this.colours = colours;
}
/**
* Test harness
* @param args not used
*/
public static void main ( String [] args )
{
Breed myDog = Breed.DALMATIAN;
// since the DALMATION inner class of Breed is anonymous,
// we have no way of getting at the getSpotCount method.
// This won't work:
// System.out.println( myDog.getSpotCount() );
}

}
 
T

Thomas Hawtin

Roedy said:
* example use of Enum, with method implemented a different way for
each enum
* constant.
Breed myDog = Breed.DALMATIAN;
// since the DALMATION inner class of Breed is anonymous,
// we have no way of getting at the getSpotCount method.
// This won't work:
// System.out.println( myDog.getSpotCount() );

It's almost a pity particular enum constant classes can't implement
different interfaces. Certainly the whole structure could be made a bit
more lax.

Two solutions stand out to me. The obvious one is to put the method in
the base enum, and throw an exception or return a special value (like
null) if called on inappropriate objects. I prefer static type safety.

The other way around is to make Breed (or BreedI) an interface. Have two
separate enums (or even just classes that follow the old enum idiom) for
the separate cases. You do lose some of the enum "sugar".

So:

interface Breed {
PlainBreed DACHSHUND = PlainBreed .DACHSHUND;
PlainBreed LABRADOR = PlainBreed .LABRADOR ;
SpottedBreed DALMATIAN = SpottedBreed.DALMATIAN;
(Bit nasty this, having a cyclic dependency between super and subtypes.
Bet there is a way to get a class-loading deadlock like this.)

String getColours();
}

enum PlainBreed implements Breed {
DACHSHUND("brown" ),
LABRADOR ("black or tan" ),
;
private final String colours;
...
}

enum SpottedBreed implements Breed {
DALMATIAN("spotted", 50),
;
private final String colours;
... copy & paste ...
}

Possibly not an extremely uncommon thing to do. I came across an example
a couple months ago with WeekDay and WeekendDay.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top