User-Defined Enumeration Types (J#) 

Visual J# provides support for authoring enumeration types. An enumeration type is an int that contains named constants to represent different possible values.

An enumeration is a user-defined value type. The rules for the semantics of value types apply. For more information, see User-Defined Value Types.

A declaration of an enumeration type is similar to a class declaration. However, the following additional rules apply:

  • The keyword enum is used in place of the class keyword.

  • Enumeration types implicitly extend Enum.

  • Enumeration types are implicitly static and final.

  • Enumeration types cannot be declared abstract.

  • Enumeration types can be public or private or package-scoped, but not protected.

  • Enumeration types cannot have methods, properties, or events.

  • Enumeration types cannot implement interfaces.

  • Enumeration types are not available with the /x (Disable Language Extensions) option.

The named values of an enumeration type are declared as static final members of the enumeration and are initialized with constant expressions, as shown in the following example:

// enum1.jsl
// compile with: /target:library
public enum ColorEnum
{
    Red,
    Green,
    Blue
}

An enumeration value can be referenced like a static field, as in the following:

int i = (int) ColorEnum.Red

It is an error to attempt to instantiate an enumeration value using the new keyword.

The integral value of an enum member can be set using the following syntax:

public enum ColorEnum
{
    Red(1),
    Green(2),
    Blue(3)
}

If not set explicitly, the integral values are set implicitly. The first enumeration value in the enumeration declaration is set to 0. Any subsequent members, if not set explicitly, are determined from the previous member's value incremented by one. For example, in the following declaration:

public enum ColorEnum
{
   Red,
   Green(10),
   Blue
}

Red gets the value 0, Green gets the value 10, and Blue gets the value 11.

Multiple enumeration type members can share the same integral value.

Dependencies of members of an enum to other members is allowed, as long as the dependency is not circular.

// enum2.jsl
// compile with: /target:library
public enum ColorEnum
{
   Red,
   Blue,
   Green(Blue);
}

Inherited methods and properties of Enum can be used on values of an enumeration type.

Enumeration types cannot be implicitly converted to their underlying type or any other integral or enumeration type. They must be explicitly converted using a cast. The following is illegal:

int i = Color.Red; // error
int i = (int) Color.Red;  // OK

The following operators can be used on values of enumeration types: ==, !=, <, >, <=, >=, +, -, ^, &, |, ~.

Enumeration types are value types and participate in boxing conversions as other value types do.

Enumeration types may be used in a switch statement. The enumeration members may be used as case labels.

ColorEnum color = Color.Red;
switch( color )
{
    case ColorEnum.Red:
    // ...
    break;
    case ColorEnum.Blue:
    // ...
    break;
    case ColorEnum.Green:
    //...
    break;
    default:
    break;
}

Each enum type defines a distinct type; an explicit enumeration conversion is required to convert between two enum types. The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type, and is a distinct value of that enum type.

Like other value types, enumeration types do not support Java reflection and cannot be serialized as Java types. Use .NET Framework reflection and .NET Framework serialization for these types.

Example

// enum_example.jsl
import System.*;

public enum RGBColor
{
    Red,
    Blue,
    Green
}

public final class RGB
{
    private int[] m_colors = new int[3];

    public RGB()
    {
        for (RGBColor rgb1 = RGBColor.Red;
            (int)rgb1 <= (int)RGBColor.Green;
            rgb1 = (RGBColor)((int)rgb1 + 1))
        {
        m_colors[(int)rgb1] = 0;
        }
    }

    public RGB(int r1, int r2, int r3)
    {
        // Enums must be converted to an integral type 
        // for use as array indices.
    m_colors[(int)RGBColor.Red] = r1;
    m_colors[(int)RGBColor.Green] = r2;
    m_colors[(int)RGBColor.Blue] = r3;
    }

    int get_Color(RGBColor c)
    {
    return m_colors[(int)c];
    }

    public static void printRGBColor(RGBColor c)
    {
    switch( c )
    {
        case RGBColor.Red:
        Console.WriteLine("Red");
        break;
        case RGBColor.Blue:
             Console.WriteLine("Blue");
            break;
        case RGBColor.Green:
            Console.WriteLine("Green");
            break;
        default:
            break;
    }

    }
    
}

public class CMain
{
    public static void main()
    {
    RGB black = new RGB();
    RGB white = new RGB(255, 255, 255);

    Console.WriteLine("Black consists of R: " + 
    black.get_Color(RGBColor.Red)
        + " G: " + black.get_Color(RGBColor.Green) 
        + " B: " + black.get_Color(RGBColor.Blue));
    
    Console.WriteLine("White consists of R: " + 
    white.get_Color(RGBColor.Red)
        + " G: " + white.get_Color(RGBColor.Green) 
        + " B: " + white.get_Color(RGBColor.Blue));

    RGB.printRGBColor( RGBColor.Red);
  }
}

Output

Black consists of R: 0 G: 0 B: 0
White consists of R: 255 G: 255 B: 255
Red

See Also

Reference

Using Enumerations
enum Keyword
Syntax for Targeting the .NET Framework
Property and Event Exposure