Saturday, May 1, 2010

Introduction to Enums


Ghar Builder

JAVA comes with eight built-in primitive types and a large set of types that are defined
by classes, such as String. But even this large collection of types is not sufficient
to cover all the possible situations that a programmer might have to deal with.
So, an essential part of JAVA, just like almost any other programming language, is the
ability to create new types. For the most part, this is done by defining new classes.
But we will look here at one particular case: the ability to define enums (short for
enumerated types). Enums are a recent addition to JAVA. They were only added in
Version 5.0. Many programming languages have something similar.

Technically, an enum is considered to be a special kind of class. In this section, we
will look at enums in a simplified form. In practice, most uses of enums will only need
the simplified form that is presented here.
An enum is a type that has a fixed list of possible values, which is specified
when the enum is created.
In some ways, an enum is similar to the boolean data type, which has true and false
as its only possible values. However, boolean is a primitive type, while an enum is
not.
The definition of an enum types has the (simplified) form:
enum enum−type−name { list−of−enum−values };
This definition cannot be inside a method. You can place it outside the main()
method of the program. The enum−type−name can be any simple identifier. This
identifier becomes the name of the enum type, in the same way that “boolean” is the
name of the boolean type and “String” is the name of the String type. Each value in
the list−of−enum−values must be a simple identifier, and the identifiers in the list
are separated by commas. For example, here is the definition of an enum type named
Season whose values are the names of the four seasons of the year:
enum Season { SPRING, SUMMER, AUTUMN, WINTER };
By convention, enum values are given names that are made up of upper case letters,
but that is a style guideline and not a syntax rule. Enum values are not variables.
Each value is a constant that always has the same value. In fact, the possible
values of an enum type are usually referred to as enum constants.
Note that the enum constants of type Season are considered to be “contained in”
Season, which means–following the convention that compound identifiers are used
for things that are contained in other things–the names that you actually use in your
program to refer to them are Season.SPRING, Season.SUMMER, Season.AUTUMN, and
Season.WINTER.
Once an enum type has been created, it can be used to declare variables in exactly
the same ways that other types are used. For example, you can declare a variable
named vacation of type Season with the statement:
Season vacation;
After declaring the variable, you can assign a value to it using an assignment
statement. The value on the right-hand side of the assignment can be one of the enum
constants of type Season. Remember to use the full name of the constant, including
“Season”! For example: vacation = Season.SUMMER;.
You can print an enum value with the statement: System.out.print(vacation).
The output value will be the name of the enum constant (without the “Season.”). In
this case, the output would be “SUMMER”.
Because an enum is technically a class, the enum values are technically objects.
As objects, they can contain methods. One of the methods in every enum value is
ordinal(). When used with an enum value it returns the ordinal number of the
value in the list of values of the enum. The ordinal number simply tells the position
of the value in the list. That is, Season.SPRING.ordinal() is the int value
0, Season.SUMMER.ordinal() is 1, while 2 is Season.AUTUMN.ordinal(), and 3 is
Season.WINTER.ordinal() is. You can use the ordinal() method with a variable of
type Season, such as vacation.ordinal() in our example.
You should appreciate enums as the first example of an important concept: creating
new types. Here is an example that shows enums being used in a complete
program:
public class EnumDemo {
/ / Define two enum types−−d e f i n i t i o n s go OUTSIDE The main ( ) r o u t i n e !
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }
public static void main(String[] args) {
Day tgif; / / Declare a v a r i a b l e of type Day .
Month libra; / / Declare a v a r i a b l e of type Month .
tgif = Day.FRIDAY; / / Assign a value of type Day to t g i f .
libra = Month.OCT; / / Assign a value of type Month to l i b r a .
System.out.print("My s ign i s l ibra , since I was born in ");
System.out.println(libra); / / Output value w i l l be : OCT
System.out.print(" That ’ s the ");
System.out.print( libra.ordinal() );
System.out.println("−th month of the year . ");
System.out.println(" (Counting from 0 , of course ! ) ");
System.out.print(" I sn ’ t i t nice to get to ");
System.out.println(tgif); / / Output value w i l l be : FRIDAY
System.out.println( tgif + " i s the " + tgif.ordinal()
+ "−th day of the week. "); / / Can concatenate enum values onto St r ings !
}
}