Java Program to Define Ordinal() Method Using Enum Concept
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.
public final int ordinal();
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:
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.
Student Id List: james : 0 peter : 1 sam : 2 --------------------------- student Name : james: 3413 student Name : peter: 34 student Name : sam: 4235