How to work with enumerations

An enumeration is a set of related constants that define a type.

The constants are defined with the int type and are assigned values from 0 to the number of constants in the enumeration minus 1.

Enumerations are type safe.

public enum EnumerationName
{
CONSTANT_NAME1 [,
CONSTANT_NAME2] . . .
}
public enum ShippingType
{
UPS_NEXT_DAY,
UPS_SECOND_DAY,
UPS_GROUND
public String toString()
{
if (this.ordinal() ==0)
return "UPS Next day";
if (this.ordinal() ==1)
return "UPS 2nd day";
return "UPS ground";
}
}
ShippingType shippingType = ShippingType.UPS_NEXT_DAY;
String shippigTypeString = ShippingType.UPS_NEXT_DAY.toString();

Good example:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

Suppose you want to add data and behavior to an enum. For example consider the planets of the solar system. Each planet knows its mass and radius, and can calculate its surface gravity and the weight of an object on the planet. Here is how it looks:

public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double mass() { return mass; }
public double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
public double surfaceGravity() {
return G * mass / (radius * radius);
}
public double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}

The enum type Planet contains a constructor, and each enum constant is declared with parameters to be passed to the constructor when it is created.

Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):

public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}

$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
Your weight on PLUTO is 11.703031


// find max from an Enum
// given this enum:
public enum Foo{
Fizz = 3,
Bar = 1,
Bang = 2}
// this gets Fizzvar last
Foo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Last();

===============================

switch (PNCTransactionState.valueOf(getTransactionState().getTranState())) {
case PNC_QUEUE_DOWN:
transactionState.setDispositionType(TranStateDispositionType.RECOVERABLE);
break;
case XML_FILE_CREATE_EXCEP:
transactionState.setDispositionType(TranStateDispositionType.RECOVERABLE);
break;
default:
transactionState.setDispositionType(TranStateDispositionType.UNRECOVERABLE);
break;
}