dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 23, 2018

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Usage of a Custom Ordinal in Enum

The getCustomValue() method on the enum returns the custom value associated. You can use this mechanism when you want to override the default ordinal with a custom ordinal.

Code snippet

public class CustomEnumOrdinal
{
   public static void main(String args[])
   {
      CustomEnumOrdinal customEnumOrdinal = new CustomEnumOrdinal();
      customEnumOrdinal.proceed();
   }
   
   enum Days {
      SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5), FRIDAY(6), SATURDAY(7);
      
      int customValue;
      Days(int customValueArg)
      {
         customValue = customValueArg;
      }
      
      int getCustomValue()
      {
         return customValue;
      }
   }
   
   private void proceed()
   {
      Days ordinalDay = Days.FRIDAY; //Change this value to one of the values in the enum Days to get the respective custome ordinal value
      System.out.println("Custom ordinal of " + ordinalDay + " is " + ordinalDay.getCustomValue());
   }
}

/*

Expected output: Custom ordinal of FRIDAY is 6

*/
M S Sridhar
 
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