Title: Method for returning previous enum value in Java
Tags: java;enums
Question: I've got the following enum:
enum Rank{TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN,
KING, ACE}
and i'm looking to create two methods. One that returns the previous value. For example, if the method is called on FOUR, THREE is returned. And another that returns the integer value. For example, TWO returns 2, JACK, QUEEN and KING return 10 and ACE returns 11.
Thanks in advance


Here is another answer: The first problem can be solved by adding method (like getValue in my further listing) to the Rank enum. It is not the only solution - for example, static factory method can also be used.
For the second problem you should introduce to the Rank enum new constructor that accepts int value and then use this constructor in enum instances declarations.
Now let's combine these two changes with your original enum:
enum Rank {
TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7),
EIGHT(8), NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);
private int value;
//private constructor which accepts int value for each card.
//It means that ACE(11) call creates ACE instance which holds value of 11 inside
Rank(int i) {
value = i;
public int getValue() {
return value;
public Rank getPrevRank(){
return values()[ordinal() &gt; 0 ? ordinal()  - 1 : 0];
}
Now you can just write:
System.out.println(Rank.FIVE.getPrevRank());//prints FOUR
System.out.println(Rank.ACE.getValue());//prints 11
System.out.println(Rank.FIVE.getPrevRank().getValue());//prints 4
PS: for your first problem you can also use this util method without any changes in Rank enum itself:
public static Rank getPreviousRank(Rank currentRank){
return Rank.values()[currentRank.ordinal() &gt; 0 ? currentRank.ordinal()  - 1 : 0];
Example:
System.out.println(getPreviousRank(Rank.FIVE)); //prints FOUR
Comment for this answer: Hi, is there a way of doing the getPrevious method within the enum? This was really  helpful by the way. Thank you!!
Comment for this answer: Static utility methods are not a good solution to anything; they only add complexity to your system and make it more difficult to test. The solution that adds the method to the enum is absolutely fine all by itself.
Comment for this answer: Of course we can do it, my friend. See fixed answer =)
Comment for this answer: you are absolutely right, Dollery. Static utility method is an absolute anti-pattern but can help to deal with some legacy code, which cannot be changed (for example). That's why I added this option in the answer :)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/22/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><enums>', 'Title': 'Method for returning previous enum value in Java', 'ViewCount': 944, 'id': 36158888, 'user_AboutMe': None, 'user_AccountId': 7057492.0, 'user_DisplayName': 'Tyler Bell', 'user_Reputation': 107.0, 'user_Views': 40.0, 'user_id': 5666695.0}}