Javatpoint Logo
Javatpoint Logo

Java Enum ordinal() Method

The ordinal() method of Enum class returns the ordinal of this enumeration constant.

This method is designed to be used by sophisticated enum-based data structures, like EnumSet and EnumMap.

Syntax

Return Value

The ordinal() method returns this enumeration constant's ordinal.

Example 1

Test it Now

Output:

Red ordinal = 0
 Green Ordinal = 1
 Brown Ordinal = 2
 Yellow Ordinal = 3

Example 2

Test it Now

Output:

First name = Reema
Last name = Panda
Position in class = 1

First name = Himanshu
Last name = Bhardwaj
Position in class = 2

First name = Geetanjali
Last name = Sharma
Position in class = 3

Example 3

Test it Now

Output:

Movies of Shahrukh Khan:
0 Don
1 DDlJ
2 Fan
3 Zero
4 VeerZara
Q In which movie Shah rukh khan has played a dual role ?
Ans. 3 Zero
Sorry!You Failed.
 The Correct answer is Don.
Next TopicJava Enum



Help Others, Please Share

facebook twitter pinterest