3

Let's say I have an Enum with BOB and ALICE and I'm storing their age in a field. Can I have methods within the enum that change their age? It seems the compiler takes it but are there any risks / is it bad practice? So for instance:

public enum Friends {
    BOB(14),
    ALICE(22);

    private int age;

    private Friends(int age){
        this.age = age;
    }

    public void setAge(int age){
        this.age = age;
    }

    public int getAge(){
       return age;
    }
}
alex314159
  • 3,159
  • 2
  • 20
  • 28
  • Have you tried same? – Panther Dec 17 '16 at 14:26
  • 2
    Variables in enums are global variables, and should be used with caution. They should be constants, and thus immutable. Otherwise, you might end up with strange bugs that are impossible to debug. If you feel the need to change a variable inside an enum, you are probably using enums wrong. – Tobb Dec 17 '16 at 14:26
  • Your question sounds like it may in fact be an [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask "how do I fix this code" when the real solution is to use a different approach entirely. While yes, you can change the state of the fields that the enum holds, I agree with @Tobb, doing this doesn't *smell* right to me, and perhaps you wish to use a different construct such as an EnumMap, something to associate each enum with a potentially mutable object. – Hovercraft Full Of Eels Dec 17 '16 at 14:34
  • Some similar questions can be found [here](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+enum+with+mutable+fields) – Hovercraft Full Of Eels Dec 17 '16 at 15:38
  • Thanks for the link @HovercraftFullOfEels. I shall look into EnumMap! – alex314159 Dec 17 '16 at 22:17
  • @HovercraftFullOfEels regarding XY Problem - my real case is that my enums have a property that depends on time (the day of the year). My initial thought was to recalculate the value of that property every morning, but I guess I will keep it as a method that other classes call, so that every property in the enums stays constant – alex314159 Dec 18 '16 at 13:58

1 Answers1

6

Java: is it permissible to change Enum fields at runtime?

You cannot change the Enum itself but yes you can change any field defined in a enum value.

Can I have methods within the enum that change their age? It seems the compiler takes it but are there any risks / is it bad practice?

Yes you can but it seems a clumsy use of Enums.

Enums are designed to represent a stable and fixed enumeration of "things".
You can consider them as constants.
From the Oracle documentation :

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

With Java Enums, these things may themselves be described by properties.

When you define your enum, you do it :

public enum Friends {
    BOB(14),
    ALICE(22);

    private int age;

    private Friends(int age){
        this.age = age;
    }

Your things are some Friends and age is a property of Friend.
The problem with your enum is that your things seem not to be stable things : the friendship is something that moves and the property you use to describe the enum : age is not a stable thing either.

In your example, your Friends look like instances with a state.
So defining classes (opposed to Enum) to represent Friends and instantiating and modifying them in the client code seems a more natural way to represent and act on them.

davidxxx
  • 125,838
  • 23
  • 214
  • 215