By Barry Burd

Life is filled with small sets of possibilities, and Java has a feature that can help you deal with these possibilities. The Java feature is called an enum type. It’s available beginning with Java version 5.0 and onward.

Consider who might win a baseball game.

image0.jpg

import java.util.Scanner;
import static java.lang.System.out;
class Scoreboard {
 enum WhoWins {home, visitor, neither}
 public static void main(String args[]) {
  Scanner keyboard = new Scanner(System.in);
  int hankees, socks;
  WhoWins who;
  out.print("Hankees and Socks scores? ");
  hankees = keyboard.nextInt();
  socks = keyboard.nextInt();
  out.println();
  if (hankees > socks) {
   who = WhoWins.home;
   out.println("The Hankees win :-)");
  } else if (socks > hankees) {
   who = WhoWins.visitor;
   out.println("The Socks win :-(");
  } else {
   who = WhoWins.neither;
   out.println("It's a tie :-|");
  }
  out.println();
  out.println("Today's game is brought to you by");
  out.println("SnitSoft, the number one software");
  out.println("vendor in the Hankeeville area.");
  out.println("SnitSoft is featured proudly in");
  out.println("Chapter 6. And remember, four out");
  out.println("of five doctors recommend");
  out.println("SnitSoft to their patients.");
  out.println();
  if (who == WhoWins.home) {
   out.println("We beat 'em good. Didn't we?");
  }
if (who == WhoWins.visitor) {
   out.println("The umpire made an unfair");
   out.println("call.");
  }
  if (who == WhoWins.neither) {
   out.println("The game goes into overtime.");
  }
  keyboard.close();
 }
}

Here’s what happens:

  • You create a variable to store values of typeWhoWins.

    Just as the line

    double amount;

    declares amount to store double values (values like 5.95 and 30.95), the line

    WhoWins who;

    declares who to store WhoWins values (values like home, visitor, and neither).

  • You assign a value to the who variable.

    You execute one of the

    who = WhoWins.something;

    assignment statements. The statement that you execute depends on the outcome of the if statement’s hankees > socks comparison.

    who = home; //This assignment doesn't work!

    the compiler gives you a home cannot be resolved to a variable error message. That’s just the way enum types work.

  • You compare the variable’s value with each of theWhoWinsvalues.

    In one if statement, you check the who == WhoWins.home condition. In the remaining two if statements, you check for the other WhoWins values.

Near the end, you could have done without enum values. You could have tested things like hankees > socks a second time.

if (hankees > socks) {
 out.println("The Hankees win :-)");
}
// And later in the program...
if (hankees > socks) {
 out.println("We beat 'em good. Didn't we?");
}

But that tactic would be clumsy. In a more complicated program, you may end up checking hankees > socks a dozen times. It would be like asking the same question over and over again.

Instead of repeatedly checking the hankees > socks condition, you store the game’s outcome as an enum value. Then you check the enum value as many times as you want. That’s a very tidy way to solve the repeated checking problem.