VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-define-ordinal-method-using-enum-concept/

⇱ Java Program to Define Ordinal() Method Using Enum Concept - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Define Ordinal() Method Using Enum Concept

Last Updated : 27 Nov, 2020

Java enum, also called Java enumeration type, is a type whose fields consist of a fixed set of constants. 

The java.lang.Enum.ordinal() tells about the ordinal number(it is the position in its enum declaration, where the initial constant is assigned an ordinal of zero) for the particular enum.

ordinal() method is a non-static method, it means that it is accessible with the class object only and if we try to access the object of another class it will give the error. It is a final method, cannot be overridden.

Syntax: 

public final int ordinal();

Return Value:

This method returns the ordinal of this enumeration constant.

Default Value of the Enum is 0 and increment upto the index present in enum.

Like we have declare three index in enum class So the ordinal() value for the enum index is 0, 1, 2

Code For the Default ordinal position for Enum:


Output
Student Name:
Rohit : 0 Geeks : 1 Author : 2

We can also store the value to the index present in the enum but the ordinal value will remain the same. It will not be altered.

Code:


Output
Student Id List: 
james : 0 peter : 1 sam : 2 
---------------------------
student Name : james: 3413
student Name : peter: 34
student Name : sam: 4235
Comment