Jump to content

Enumeration (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Doug Bell (talk | contribs) at 20:49, 20 January 2006 (typo). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Enumeration is a special technique used in programming languages. In general it associates each element of an ordered list with a corresponding integer number. This allows one to work with descriptive values instead of using magic numbers.

Java (since J2SE 5.0)

In the Java programming language a program defining an enum line might look like this:

enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

It is important to note, that a variable defined in a simple way like

Days day;

is now typesafe. This means, you can only assign enum values to such a variable, not integer numbers.

There is also the possibility to assign each enum entry additional values and certain functions for enhancement. This provide additional functionality over the enum construct in C++, where enum variables are only glorified integers. Java has certain tools for operating with enums and they can be seen more like a full-featured class than a description for integers.