Java Program to Define Ordinal() Method Using Enum Concept Last Updated : 27 Nov, 2020 Comments Improve Suggest changes Like Article Like Report 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: Java // Java program to show the usage of // ordinal() method of java enumeration import java.lang.*; import java.util.*; // enum showing Student details enum Student { Rohit, Geeks,Author; } public class GFG { public static void main(String args[]) { System.out.println("Student Name:"); for(Student m : Student.values()) { System.out.print(m+" : "+m.ordinal()+" "); } } } OutputStudent 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: Java // Java program to show that the ordinal // value remainw same whether we mention // index to the enum or not import java.lang.*; import java.util.*; // enum showing Mobile prices enum Student_id { james(3413), peter(34), sam(4235); int id; Student_id(int Id) { id = Id; } public int show() { return id; } } public class GFG { public static void main(String args[]) { // printing all the default ordinary number for the // enum index System.out.println("Student Id List: "); for (Student_id m : Student_id.values()) { System.out.print(m + " : " + m.ordinal() + " "); } System.out.println(); System.out.println("---------------------------"); for (Student_id id : Student_id.values()) { // printing all the value stored in the enum // index System.out.print("student Name : " + id + ": " + id.show()); System.out.println(); } } } OutputStudent Id List: james : 0 peter : 1 sam : 2 --------------------------- student Name : james: 3413 student Name : peter: 34 student Name : sam: 4235 Comment More infoAdvertise with us Next Article Java Program to Define Ordinal() Method Using Enum Concept rohit2sahu Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Enumeration +1 More Practice Tags : Java Similar Reads Java Program to Convert Enum to String Given an enum containing a group of constants, the task is to convert the enum to a String. Methods: We can solve this problem using two methods: Using name() MethodUsing toString() Method Let us discuss both of them in detail and implementing them to get a better understanding of the same. Method 1 2 min read Java Program to Read Elements using Enumeration in Hashtable The enumeration in java is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable(like Stack, Vector, HashTable, etc.) in a forward direction only and not in the backward direction. HashTable is a class The hash table class implements a Map 3 min read Java Program to Access All the Constant Defined in the Enum An enum is a special class that represents a group of constants. To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; } values() method can be used to return all val 1 min read Java Program to Iterate Vector using Enumeration The Vector class implements a growable array of objects. It is available in java.util package. It implements the List interface. The Enumeration interface defines the methods by which you can traverse the elements in a collection of objects. Now in order to add elements Vector Syntax: public class V 2 min read How to Customize the Ordering of Elements in a PriorityQueue Using a Comparator in Java? A PriorityQueue is a data structure that allows elements to be processed based on their priority. By default, it orders elements according to their natural ordering (e.g., numeric values in ascending order). However, sometimes we need a different sorting order based on specific criteria. In this art 4 min read How to Implement a Strategy Pattern using Enum in Java? The strategy design pattern is intended to provide a way to choose from a variety of interchangeable strategies. Classic implementation includes an architecture to be implemented by each technique and offers a concrete implementation for execution. The method is chosen from an interface reference, a 2 min read Java Program to Create ArrayList From Enumeration Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay 2 min read Creating TreeSet with Comparator by User Define Objects in Java TreeSet is the implementation class of Set Interface. It follows a natural sorting order or you can customize it using a comparator and it also does not allow duplicates. Syntax: TreeSet<String> gfg= new TreeSet<>(); Below is the normal implementation of the TreeSet: Java // Java program 6 min read How to Change the Comparator to Return a Descending Order in Java TreeSet? Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the eleme 3 min read How to Use Enumeration to Display Elements of Hashtable in Java? Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. Now here we can get the keys and 2 min read Like