dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Usage of Ordinal in Enum

Learn more about the usage of the ordinal()method on the enum.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


The ordinal() method on the enum returns the constant. In other words, you can assume this to be the same as index and similar to arrays the index starts with zero(0).

Code snippet:

public class EnumOrdinal
{
   public static void main(String args[])
   {
      EnumOrdinal enumOrdinal = new EnumOrdinal();
      enumOrdinal.proceed();
   }
   
   enum Days {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
   }
   
   private void proceed()
   {
      Days ordinalDay = Days.FRIDAY; //Change this value to one of the values in the enum Days to get the respective ordinal
      System.out.println("Ordinal of " + ordinalDay + " is " + ordinalDay.ordinal());
   }
}

/*

Expected output: Ordinal of FRIDAY is 5

*/

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date