-1

I have a switch case in my code which has cases defined on String constants.
If I initialize the String constants as

String INACTIVE = "INACTIVE";  

The switch case works fine.

However, If I initialize the String constants using an enum as

String INACTIVE = State.INACTIVE.name();

I get a compile-time error on the switch case saying

Constant Expression Required

I am using MVP architecture and my State Enum can be used only in the Presenter, while my switch case is in the Activity View.

I don't want to duplicate the Enum in the View keeping in mind code maintenance issues.

This forces me to define String constants separately for my switch case, but it is not allowing me to initialize the constants using State enum values.

Thanking in advance for any helpful suggestions and solutions.

Pritam
  • 339
  • 3
  • 23
Zeba
  • 3,041
  • 1
  • 28
  • 39
  • could you please show the enum Implementations – Rahal Kanishka Aug 29 '18 at 06:02
  • [See here](https://stackoverflow.com/questions/6391777/switch-on-enum-in-java) as well as the following duplicate chain. You can't do this, but maybe you shouldn't be doing this anyway. What would be wrong with just defining string constants somewhere in a util/static class? I mean, why use an enum in the first place? – Tim Biegeleisen Aug 29 '18 at 06:05
  • 1
    this is how `Java` works. You can `case` only by a constant value, while Java can't prove that `INACTIVE.name()` will always return the same value. – Vladyslav Matviienko Aug 29 '18 at 06:10
  • 2
    @Zeba just switch against the enum, no need to switch against the String retrieved through the enum. I mean: you can associate to each enum an id (progressive int for instance is by default), or even use the name string, and pass that between the activities; then once received, let the enum class to returns you the enum by the given integer (or string), and then you can switch by enum. Enum is a very powerful class if you know how to use it. – Alessio Aug 29 '18 at 06:11

1 Answers1

1

Do it like this:

public enum State {
    INACTIVE {
        @Override
        public int getId() {
            return 0;
        }
    },
    ACTIVE {
        @Override
        public int getId() {
            return 1;
        }
    },
    CURRENT {
        @Override
        public int getId() {
            return 2;
        }
    };

    public abstract int getId();

    public State getStateById(int id) {
        if (id == 0) {
            return INACTIVE;
        } else if (id == 1) {
            return ACTIVE;
        } else if (id == 2) {
            return CURRENT;
        }
        // this should never happen
        throw new RuntimeException("Shouldn't");
    };
}

so basically you use an id to be passed between the Presenter and the ActivityView, and then use the enum class itself to resolve the id and retrieve the enum, which you can use in the switch case directly. In my example I used an int just to show you all the details, but if you rely on getName() and resolve the String directly, you don't need to overwrite any method at all.

Alessio
  • 3,063
  • 1
  • 15
  • 17