Enum ordinal() in Java

ordinal(): This method is available in java.lang.Enum class of Java.

Syntax:

int java.lang.Enum.ordinal()

This method returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

Parameters: NA

Returns: the ordinal of this enumeration constant.

Exceptions: NA

Approach

Java

enum Colour {
    RED, GREEN, GRAY, ORANGE
}

public class Enumordinal {
    public static void main(String[] args) {

        System.out.println(Colour.GRAY.ordinal());
    }
}

Output:

2


No comments:

Post a Comment