can enum values be assigned to integer values for calculations? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

can enum values be assigned to integer values for calculations?

enum Month {Jan=1,Feb=2,Mar=3,Apr=4,May=5} Is anything like this possible instead of just,, enum Month {Jan,Feb,Mar,Apr,May} I mean, if(Month==1) would be more preferable than if(Month==Jan)

23rd Jul 2016, 3:45 PM
Jo Thomas
Jo Thomas - avatar
1 ответ
+ 1
In Java enums are similar to classes, so you can do the following: public enum Something { OPEN(100), CLOSE(200); private final int id; Something(int id) { this.id = id; } public int getValue() { return id; } } See this link for more info: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
9th Aug 2016, 2:46 PM
James Flanders