0

I have an enum Type

public enum Type {
        A("abc1", "desc1"),
        B("abc2", "desc2"),
        C("abc3", "desc3"),
        D("abc4", "desc4"),
        E("abc5", "desc5");

        private String value;
        private String description;

        private Type(String value, String description) {
            this.value = value;
            this.description = description;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

I get the values from the enum through the following code

Type[] type = Type.values();

for (int i = 0; i < type.length; i++) {
    System.out.println(type[i].getValue());
}

This code runs on a server. For few days it runs fine and the output of this code is as follows :

abc1
abc2
abc3
abc4
abc5

But after few days the output changes to

abc1
abc2
abc1
abc4
abc5

If I print the description then they are always correct.

I am not able to figure out why Type.getValue() is returning value of enum Value A for enum Value C.

vinit
  • 155
  • 14
  • 5
    "But after few days the output changes to" - This made me laugh :) Are there other running threads somewhere? – Maroun Sep 29 '15 at 12:08
  • 4
    Mutable enums feel like a very bad idea. Make `value` final, remove the setter. – Andy Turner Sep 29 '15 at 12:10
  • http://stackoverflow.com/questions/3820149/enum-values-is-an-order-of-returned-enums-deterministic – Paraneetharan Saravanaperumal Sep 29 '15 at 12:10
  • 2
    sure that nobody ever calls `setValue()`? – Thomas Weller Sep 29 '15 at 12:12
  • Are you are that `setValue()` is not being called at some point by mistake? Not a good idea to have the ability to change something in an `enum`. The whole point of `enum` is to be an unchangeable list of items that you can control as the developer, to limit the options of the user to those in the list. – Nicholas Robinson Sep 29 '15 at 12:14
  • 2
    Remove setValue() and let us know if it worked in a few days. – Manu Sep 29 '15 at 12:14
  • I searched the entire package and found that setValue is being called for Type C. Now, I have removed that and made the values final in the enum – vinit Sep 29 '15 at 12:28

1 Answers1

4

Your enum is mutable; something is calling setValue().

You should make your enum immutable - it makes good sense to do this. Make it immutable by making the fields final and ditching the setters:

public enum Type {
    A("abc1", "desc1"),
    B("abc2", "desc2"),
    C("abc3", "desc3"),
    D("abc4", "desc4"),
    E("abc5", "desc5");

    private final String value;
    private final String description;

    private Type(String value, String description) {
        this.value = value;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public String getValue() {
        return value;
    }
}

}
Bohemian
  • 412,405
  • 93
  • 575
  • 722