enum as Class

In this chapter you will learn:

  1. How to use enum as Class
  2. enum type Inheriting Enum

Use enum as Class

You can give constructors, add instance variables and methods, and implement interfaces for enum types.

When you define a constructor for an enum, the constructor is called when each enumeration constant is created.

Each enumeration constant has its own copy of any instance variables defined by the enumeration.

// Use an enum constructor, instance variable, and method. 
enum Direction {
  South(10), East(9), North(12), West(8);
  private int myValue;
  // Constructor// j a va2  s.  c o  m

  Direction(int p) {
    myValue = p;
  }

  int getMyValue() {
    return myValue;
  }
}

public class Main {
  public static void main(String args[]) {

    System.out.println(Direction.East.getMyValue());

    for (Direction a : Direction.values())
      System.out.println(a + " costs " + a.getMyValue());
  }
}

Two restrictions that apply to enumerations.

  • an enumeration can't inherit another class.
  • an enum cannot be a superclass.

You can add fields, constructors, and methods to an enum. You can have the enum implement interfaces.

enum Coin {//from   j a v a2 s  .co  m
  PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
  private final int denomValue;

  Coin(int denomValue) {
    this.denomValue = denomValue;
  }

  int denomValue() {
    return denomValue;
  }

  int toDenomination(int numPennies) {
    return numPennies / denomValue;
  }
}

public class Main {
  public static void main(String[] args) {
    int numPennies = 1;
    int numQuarters = Coin.QUARTER.toDenomination(numPennies);
    numPennies = Coin.QUARTER.denomValue();
    int numDimes = Coin.DIME.toDenomination(numPennies);
    numPennies = Coin.DIME.denomValue();
    int numNickels = Coin.NICKEL.toDenomination(numPennies);
    numPennies = Coin.NICKEL.denomValue();
    for (int i = 0; i < Coin.values().length; i++) {
      System.out.println(Coin.values()[i].denomValue());
    }

  }
}

enum type Inheriting Enum

All enum inherit java.lang.Enum. java.lang.Enum's methods are available for use by all enumerations.

You can obtain a value that indicates an enumeration constant's position in the list of constants. This is called its ordinal value, and it is retrieved by calling the ordinal() method:

final int ordinal()

It returns the ordinal value of the invoking constant. Ordinal values begin at zero.

You can compare the ordinal value of two constants of the same enumeration by using the compareTo() method.

final int compareTo(enum-type e)

enum-type is the type of the enumeration, and e is the constant being compared to the invoking constant.

If the two ordinal values are the same, then zero is returned. If the invoking constant has an ordinal value greater than e's, then a positive value is returned.

You can compare for equality an enumeration constant with any other object by using equals(). Those two objects will only be equal if they both refer to the same constant, within the same enumeration.

The following program demonstrates the ordinal(), compareTo(), and equals() methods:

// Demonstrate ordinal(), compareTo(), and equals(). 
enum Direction {
  East, South, West, North//from   j a v  a2s .  c  o  m
}

public class Main {
  public static void main(String args[]) {
    Direction ap, ap2, ap3;
    // Obtain all ordinal values using ordinal().
    for (Direction a : Direction.values()){
      System.out.println(a + " " + a.ordinal());      
    }
    ap = Direction.West;
    ap2 = Direction.South;
    ap3 = Direction.West;
    System.out.println();
    // Demonstrate compareTo() and equals()
    if (ap.compareTo(ap2) < 0)
      System.out.println(ap + " comes before " + ap2);
    if (ap.compareTo(ap2) > 0)
      System.out.println(ap2 + " comes before " + ap);
    if (ap.compareTo(ap3) == 0)
      System.out.println(ap + " equals " + ap3);
    System.out.println();
    if (ap.equals(ap2))
      System.out.println("Error!");
    if (ap.equals(ap3))
      System.out.println(ap + " equals " + ap3);
    if (ap == ap3)
      System.out.println(ap + " == " + ap3);
  }
}

Next chapter...

What you will learn in the next chapter:

  1. What is the syntax for try catch finally statement
  2. How to use try and catch