Developing Enumerated Types
With Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
Though it is not immediately obvious from the documentation for the Java API, all enumerated types have the following methods "implemented for them".
values()
returns an array containing
all of the objects in the setvalueOf(String)
returns the instance with the
specified identifier (or throws
IllegalArgumentException
)int compareTo(E other)
which uses the order
of the elementsboolean equals(E other)
ordinal()
returns the ordinal value of the instance
(0-based)toString()
returns a String
representation of the identifier/** * An example that uses the Month enum */ public class MonthDriver { public static void main(String[] Args) { Month[] all, fun; fun = new Month[3]; fun[0] = Month.JUNE; fun[1] = Month.JULY; fun[2] = Month.AUGUST; System.out.println("\nThe fun months:"); for (int i=0; i<fun.length; i++) { System.out.println(fun[i].toString()); } System.out.println("\nAll months after May:"); all = Month.values(); for (int i=0; i<all.length; i++) { if (all[i].compareTo(Month.MAY) > 0) { System.out.println(all[i].toString()); } } } }
/** * An enumerated type for the months of the year * * @author Prof. David Bernstein, James Madison University * @version 2.0 */ public enum Month { JANUARY ("January", 31), FEBRUARY ("February", 28), MARCH ("March", 31), APRIL ("April", 30), MAY ("May", 31), JUNE ("June", 30), JULY ("July", 31), AUGUST ("August", 31), SEPTEMBER ("September",30), OCTOBER ("October", 31), NOVEMBER ("November", 30), DECEMBER ("December", 31); private final int days; private final String name; /** * Explicit Value Constructor. * * @param name The name of the Month * @param days The number of days in the Month */ private Month(String name, int days) { this.name = name; this.days = days; } /** * Get the 3-letter abbreviation for this Month. * * @return The 3-letter abbreviation */ public String getAbbreviation() { String result; if (name.length() > 3) result = name.substring(0,3)+"."; else result = name; return result; } /** * Get the numeric value of this Month (in the interval [1,12]). * * @return The numeric value of this Month */ public int getNumber() { return ordinal()+1; // ordinal() returns the position in the declaration } /** * Get the (normal) number of days in this Month. * * @return The normal number of days in this Month. */ public int getLength() { return days; } /** * Return the Month that corresponds to a given String. * * @param s The String representation * @return The corresponding Month (or null) */ public static Month parseMonth(String s) { Month[] all; all = Month.values(); for (int i=0; i<all.length; i++) { if (s.equalsIgnoreCase(all[i].name) || s.equalsIgnoreCase(all[i].getAbbreviation())) { return all[i]; } } return null; } /** * Get a String representation of this Month * * @return The String representation (i.e., the name) */ public String toString() { return name; } }
Month
enum/** * An example that uses the Month enum */ public class MonthDriver { public static void main(String[] Args) { int total; Month[] fun; fun = new Month[3]; fun[0] = Month.JUNE; fun[1] = Month.JULY; fun[2] = Month.AUGUST; total = 0; System.out.println("\nThe fun months:"); for (int i=0; i<fun.length; i++) { System.out.printf("%5s %3d\n", fun[i].getAbbreviation(), fun[i].getLength()); total += fun[i].getLength(); } System.out.printf("\n%5s %3d", " ", total); } }
public static final String
Objects:
public static final String
Objects:
public static final int
Values: