This documentation is archived and is not being maintained.

enum Keyword 

Visual Studio 2005

In Microsoft Visual J# 2005, it is now possible to author enumeration types using the enum keyword. It is used to declare a user-defined enumeration, which consists of a set of named constants called the enumerator list. Every enumeration type has an underlying type of int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example:

enum Day {Sat, Sun, Mon, Tue, Wed, Thu, Fri}

In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. You can display the elements of the enumeration directly, like this:

System.out.println(Day.Mon);   // Displays Mon.

You can also display the integers associated with the named constant, like this:

System.out.println((int)Day.Mon);   // Displays 2.

You can change the values of the enumeration by initializing the elements. For example:

enum Day {Sat(1), Sun, Mon, Tue, Wed, Thu, Fri}

In this enumeration, the sequence of elements is forced to start from 1 instead of 0.

It is also possible to initialize more than one element. For example, consider the enumeration:

enum DayOff {Sat(7), Sun(1)}

In this case, the value associated with Sat is 7 and the value associated with Sun is 1.

You can pass enumerations as parameters to methods. For example, you can declare a method like this:

public static void MyMethod(DayOff d)
{
    // Display the enum element as a string:
    System.out.println(d);
}

This method is called by using a call like this:

MyMethod(DayOff.Sat);   // Displays Sat.

Enumerations are type-safe; that is, there is no automatic conversion between enum and int. For example, the following code generates a compile-time error:

DayOff d1 = 1;   // Error.

You can, however, use a cast to create an integer variable from an enum element:

int d1 = (int)DayOff.Sun;   // Ok.

To find out the type from which an enum is derived, use a statement like this:

System.out.println("The " +
 DayOff.Sat.getClass().ToType() +
 " type is derived from " +
 DayOff.Sat.getClass().ToType().get_BaseType() +".");

The previous statement gives the following output:

The DayOff type is derived from System.Enum.

You can also get the underlying type by using a statement like the following statement:

System.out.println("Underlying type: " + System.Enum.GetUnderlyingType(DayOff.class.ToType()));

This statement gives the following output:

The underlying type is System.Int32.

You can apply FlagsAttribute to an enumeration to indicate that the enumeration should be treated as a bit field; that is, as a set of flags (see Example 2).

The type enum might also be used in the switch statement. The following example demonstrates such use.

// enum-ex1.jsl
// Using enum with switch.

// Declare an enum type for working days:
enum WorkingDay {Mon(1), Tue, Wed, Thu, Fri};

public class MyClass
{
    public static void main(String [] args)
    {
        // Declare an enum element of the type WorkingDay:
        WorkingDay d = WorkingDay.Fri;
        int dayNum = (int)d;

        // Test the selected day:
        switch (d)
        {
            case WorkingDay.Mon:
                System.out.println("The selected day is " + d + ".");
                System.out.println("The number of the day is " +
                (int)d + ".");
                break;
            case WorkingDay.Tue:
                System.out.println("The selected day is " + d + ".");
                System.out.println("The number of the day is " +
                (int)d + ".");
                break;
            case WorkingDay.Wed:
                System.out.println("The selected day is " + d + ".");
                System.out.println("The number of the day is " +
                (int)d + ".");
                break;
            case WorkingDay.Thu:
                System.out.println("The selected day is " + d + ".");
                System.out.println("The number of the day is " +
                (int)d + ".");
                break;
            case WorkingDay.Fri:
                System.out.println("The selected day is " + d + ".");
                System.out.println("The number of the day is " +
                dayNum + ".");
                break;
            default:
                System.out.println("Not case is suitable!");
                break;
        }
    }
}

Output

The selected day is Fri.
The number of the day is 5.

This example demonstrates using the FlagsAttribute attribute with enumerations.

// enum-ex2.jsl
// Using FlagsAttribute with enum.

import System.*;

/** @attribute Flags() */
public enum CarOptions
{
    SunRoof(0x01),
    Spoiler(0x02),
    FogLights(0x04),
    TintedWindows(0x08)
}

public class flagtest
{
    public static void main(String args[])
    {
        CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
        System.out.println(options);
        System.out.println((int)options);
    }
}

Output

SunRoof, FogLights
5

Notice that if you remove the FlagsAttribute attribute, the example will output the following:

5
5
Show: