How to make key value like enum in java

Saqib Ahmed picture Saqib Ahmed · Nov 11, 2016 · Viewed 71.9k times · Source

I need to make an Enum containing some strings with spaces and their values in int like:

public enum status{
Active(1),
Inactive(2);
}

because I am using it with hibernate and also will convert it to JSON for alpaca js forms.

like:

[{"text": "Inactive", "value":"2"},{"text": "Active", "value":"1"}]

I'm stuck in making enum. how to make such type of enum?

Answer

Chandana Kumara picture Chandana Kumara · Nov 11, 2016

You can not put space between strings. Instead of the you can use underscore as follows:

In_Active

You can use this way:

enum Status {

    ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);

    private final String key;
    private final Integer value;

    Status(String key, Integer value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }
    public Integer getValue() {
        return value;
    }
}