2

So I have a field called optionType and I want it to be able to take on 4 different integer values corresponding to certain options. For example optionType = 0 means the user wants to handle something a certain way, optionType = 1 means another way, etc.

But numbers by themselves are meaningless so I wanted to define constants instead in the class.

public class MyClass {
    public static final int OPTION_TYPE_DO_THIS = 0;
    public static final int OPTION_TYPE_DO_THAT = 1;
    public static final int OPTION_TYPE_DO_SOMETHING_ELSE = 2;
    public static final int OPTION_TYPE_DO_COOL_THING = 3;
    private int optionType;

    ....

Is it considered normal to define all the constants out like that or is it better to use an enum like

public enum OPTION_TYPE {DO_THIS, DO_THAT, DO_SOMETHING_ELSE, DO_COOL_THING};

Or am I supposed to be using Enum instead somehow?

3 Answers3

1

The key point is more on "how will that information be used at runtime". You see, if you starting thinking about writing code such as

switch(someEnum) {
  case DO_THIS: ...
  case DO_THAT:

... then you are already going into the wrong direction!

The point is: very often think that enums (or their even-more-low-level numeric constant cousins) are a good way to express such designs.

But that actually leads to quite some problems. Very often, the better, "more OO" way of thing is to use some abstract base class with specific subclasses; in other words: polymorphism!

Edit: just to make that more clear ... a "single" switch over an enum isn't really a problem. But far too often, people end up with many many places in code where they switch over their enums. And all of those places might need updates when you create additional enum constants. A coworker of mine calls that the "enum trap".

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • That switch statement is indeed exactly what I was planning to do; why is that the wrong direction? What's the harm? How do you make such a small thing like this "polymorphic"? – user7086932 Nov 02 '16 at 19:04
  • Googled around a bit, are you saying something like this? http://stackoverflow.com/a/6392518/7086932 – user7086932 Nov 02 '16 at 19:10
  • 1
    You are welcome! And a newbie using google to refine an answer ... that is worth a double upvote ;-) – GhostCat Nov 02 '16 at 19:32
0

Take a look at this question and answer,

Even though it is written in the C# context, the conclusion states that:

  • Enums are great for lightweight state information.
  • Static class members would be able to support this multiple state without any extra functionality.
Community
  • 1
  • 1
marcelovca90
  • 2,673
  • 3
  • 27
  • 34
0

In java enums are more than just "enumerated names" as they are in other language (e.g. in C/C++).

My preferred use is to provide stateless behavior:

class Calculator {
  enum Operation {
     ADD{
        double calculate(double a, double b){ return a + b;}
     }
     SUB{
        double calculate(double a, double b){ return a - b;}
     }
     MUL{
        double calculate(double a, double b){ return a * b;}
     }
     DIV{
        double calculate(double a, double b){ return a / b;}
     }
     abstract double calculate(double a, double b);
  }

  Map<String,Operation> operations = new HashMap<>();
  Calculator(){
    operations.put("+",ADD);
    operations.put("-",SUB);
    operations.put("*",MUL);
    operations.put("/",DIV);
  }

  public double calculate(double a, String operation, double b){
    return operations.get(operation).calculate(a,b);
  }
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51