What is the purpose of the 'ordinal()' method in the StudentResult enum?

QuestionsQuestions8 SkillsProJava Enum FundamentalsSep, 20 2025
099

The ordinal() method in an enum returns the zero-based index of the enum constant in the order they are declared. For example, in the StudentResult enum:

public enum StudentResult {
    PASS,   // ordinal 0
    FAIL,   // ordinal 1
    ABSENT  // ordinal 2
}
  • StudentResult.PASS.ordinal() would return 0
  • StudentResult.FAIL.ordinal() would return 1
  • StudentResult.ABSENT.ordinal() would return 2

This method is useful when you need to determine the position of an enum constant within its declaration order.

0 Comments

no data
Be the first to share your comment!