dev ツ dev ツ - 1 month ago 7
Java Question

How to set enum on the basis of integer value in Java?

I have a

enum


private enum Order {
DESC, ASC
}


I need set this enum on the basis of
int
value



  • 0 ==> DESC

  • 1 ==> ASC




public void setOrder(int order) {
// some code
}

Answer

As long as you have increasing value starting from 0 and the order in your enum is identical to the number ordering you can use the values() array:

Order desc = Order.values()[0];
Order asc = Order.values()[1];