Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Enum return Int

Tags:

java

enums

I'm having trouble declaring an enum. What I'm trying to create is an enum for a 'DownloadType', where there are 3 download types (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 then use it like this:

DownloadType.AUDIO_AND_VIDEO.value; 

However, I would like it so that I don't have to ask for the 'value'. I may be mistaken, but this is the way several classes work in Java such as Font, for example to set a font style, you use:

Font.PLAIN 

Which returns an int value, we don't use:

Font.PLAIN.value 
like image 449
Cristian Avatar asked Dec 09 '12 21:12

Cristian


People also ask

How do I return an enum from an int?

Getting Integer from Enum On the other hand, to get the integer value from an enum, one can do as follows, by using the getValue method. The getValue method simply returns the internal value of the enum that we store within the value variable.

Can enums be integers Java?

No, we can have only strings as elements in an enumeration.

How do I return an enum in Java?

How can I return enums like this? on a sidenote, according to java conventions enums should start with an upper case letter. An enum is a (special type of) class, so you should declare it as the return type of your method. By the way, it would be better to name it Decision (it is a class).

What does enum valueOf return?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)


2 Answers

Font.PLAIN is not an enum. It is just an int. If you need to take the value out of an enum, you can't avoid calling a method or using a .value, because enums are actually objects of its own type, not primitives.

If you truly only need an int, and you are already to accept that type-safety is lost the user may pass invalid values to your API, you may define those constants as int also:

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 an .ordinal() method, so you could define the enum as:

enum DownloadType { AUDIO, VIDEO, AUDIO_AND_VIDEO } 

and get the "value" using

DownloadType.AUDIO_AND_VIDEO.ordinal() 

Edit: Corrected the code.. static class is not allowed in Java. See this SO answer with explanation and details on how to define static classes in Java.

like image 89
kennytm Avatar answered Sep 22 '22 11:09

kennytm


If you need to get the int value, just have a getter for the value in your 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 simple use the ordinal() method, which would return the position of the enum constant in the 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 
like image 24
PermGenError Avatar answered Sep 23 '22 11:09

PermGenError