Java Enum return Int

I am having trouble declaring an listing. What I'm trying to create is an enumeration for "DownloadType" where there are three types of downloads (AUDIO, VIDEO, AUDIO_AND_VIDEO).

I have implemented the code as follows:

private enum DownloadType { AUDIO(0), VIDEO(1), AUDIO_AND_VIDEO(2); private final int value; private DownloadType(int value) { this.value = value; } } 

This works fine if I use it like this:

 DownloadType.AUDIO_AND_VIDEO.value; 

However, I would like that I do not need to request a value. Maybe I'm wrong, but this is how some classes work in Java, such as Font, for example, to set the font style, you use:

 Font.PLAIN 

Which returns the value int, we do not use:

 Font.PLAIN.value 
+65
java enums
Dec 09 '12 at 21:35
source share
7 answers

Font.PLAIN is not an enumeration. It is just an int . If you need to extract a value from an enumeration, you cannot avoid calling a method or using .value , because enumerations are actually objects of their own type, not primitives.

If you really only need int , and you already agree that type safety is lost, the user can pass invalid values ​​to your API, you can also define these constants as int :

 public final class DownloadType { public static final int AUDIO = 0; public static final int VIDEO = 1; public static final int AUDIO_AND_VIDEO = 2; // If you have only static members and want to simulate a static // class in Java, then you can make the constructor private. private DownloadType() {} } 

By the way, the value field is actually redundant because there is also a .ordinal() method, so you can define enum as:

 enum DownloadType { AUDIO, VIDEO, AUDIO_AND_VIDEO } 

and get the "value" using

 DownloadType.AUDIO_AND_VIDEO.ordinal() 



Edit : fixed the code .. static class not allowed in Java. See this SO answer for an explanation and details on how to define static classes in Java.

+93
Dec 09
source share

If you need to get the int value, just get getter for the value in ENUM:

 private enum DownloadType { AUDIO(1), VIDEO(2), AUDIO_AND_VIDEO(3); private final int value; private DownloadType(int value) { this.value = value; } public int getValue() { return value; } } public static void main(String[] args) { System.out.println(DownloadType.AUDIO.getValue()); //returns 1 System.out.println(DownloadType.VIDEO.getValue()); //returns 2 System.out.println(DownloadType.AUDIO_AND_VIDEO.getValue()); //returns 3 } 

Or you could just use the ordinal() method, which would return the position of an enum constant to an enum.

 private enum DownloadType { AUDIO(0), VIDEO(1), AUDIO_AND_VIDEO(2); //rest of the code } System.out.println(DownloadType.AUDIO.ordinal()); //returns 0 System.out.println(DownloadType.VIDEO.ordinal()); //returns 1 System.out.println(DownloadType.AUDIO_AND_VIDEO.ordinal()); //returns 2 
+47
Dec 09
source share

First you have to ask yourself the following question: do you really need an int?

The purpose of enumerations is to have a collection of elements (constants) that have a value in the code, without relying on an external value (for example, int). Enumerations in Java can be used as an argument for switch parameters, and they can be safely compared using the equality operator "==" (among others).

Proposal 1 (unnecessarily):

Often there is no need for a whole number, just use this:

 private enum DownloadType{ AUDIO, VIDEO, AUDIO_AND_VIDEO } 

Using:

 DownloadType downloadType = MyObj.getDownloadType(); if (downloadType == DownloadType.AUDIO) { //... } //or switch (downloadType) { case AUDIO: //... break; case VIDEO: //... break; case AUDIO_AND_VIDEO: //... break; } 

Proposal 2 (necessary):

However, it can sometimes be useful to convert enum to int (for example, if the external API expects an int value). In this case, I would advise marking methods as conversion methods using toXxx() -Style. To print, override toString() .

 private enum DownloadType { AUDIO(2), VIDEO(5), AUDIO_AND_VIDEO(11); private final int code; private DownloadType(int code) { this.code = code; } public int toInt() { return code; } public String toString() { //only override toString, if the returned value has a meaning for the //human viewing this value return String.valueOf(code); } } System.out.println(DownloadType.AUDIO.toInt()); //returns 2 System.out.println(DownloadType.AUDIO); //returns 2 via 'toString/code' System.out.println(DownloadType.AUDIO).ordinal(); //returns 0 System.out.println(DownloadType.AUDIO.name()); //returns AUDIO System.out.println(DownloadType.VIDEO.toInt()); //returns 5 System.out.println(DownloadType.VIDEO.ordinal()); //returns 1 System.out.println(DownloadType.AUDIO_AND_VIDEO.toInt()); //returns 11 

Summary

  • Do not use Integer with enum if it is not needed.
  • Do not rely on using ordinal() to get an integer enumeration, because this value can change if you change the order (for example, by inserting a value). If you plan on using ordinal() , it might be better to use sentence 1.
  • Usually do not use int constants instead of enumerations (as in the accepted answer), because you will lose type safety.
+12
Mar 12 '14 at 11:39
source share

Just call the ordinal() method on the enumeration value to get its corresponding number. There is no need to declare an add attribute with its value, each enumerated value gets its own default number, is assigned starting from zero, increasing by one for each value in the same order in which they were declared.

You should not depend on the value of int enum , only on its actual value. Enumerations in Java are another type of monster and are not like enums in C, where you depend on their integer code.

Regarding the example you provided in the question, Font.PLAIN works because it is just an integer constant of the Font class. If you absolutely need a (possibly changing) digital code, then enum not the right tool for the task, it is better to stick to numerical constants.

+10
Dec 09 '12 at 21:40
source share

You can try this code.

 private enum DownloadType { AUDIO , VIDEO , AUDIO_AND_VIDEO ; } 

You can use this enumeration as follows: DownloadType.AUDIO.ordinal() . Hope this code snippet helps you.

+4
Nov 23 '14 at 10:24
source share

If you combine an enumeration with a string, you can override the toString method to return int:

 public String toString() { return value + ""; } 

Then you can simply use:

 String something = "foo" + DownloadType.AUDIO; 

and the toString () method will be called.


Please note that using toString () programmatically is usually considered bad practice - it is intended only for human eyes, however this is the only way to achieve what you ask for.

+3
Dec 09
source share

Do you want this code?

 public static enum FieldIndex { HDB_TRX_ID, //TRX ID HDB_SYS_ID //SYSTEM ID } public String print(ArrayList<String> itemName){ return itemName.get(FieldIndex.HDB_TRX_ID.ordinal()); } 
+3
Mar 26 '15 at 8:19
source share



All Articles