switch not recognizing ENUM as constant

Asked

Viewed 55 times

-1

public enum enumUpdateAction {
    NEW(0), CHANGE(1), DELETE(2), DELETE_THRU(3), DELETE_FROM(4);

    public int codigo;

    enumUpdateAction(int codigo) {
        this.codigo = codigo;
    }

    public int getCodigo() {
        return this.codigo;
    }
}

...

switch (Eventos.get(0).getUpdateaction()) {

            case enumUpdateAction.NEW.getCodigo(): 
                ArrayEventos.add(InserirOrdenado(ArrayEventos, Ordem), Ordem);
                break;
}

After creating Enum, I am trying to use this value inside a switch (in this case, NEW corresponds to 0 and therefore I am trying to use 0 inside the case), but I get the error "Constant Expression required". Can anyone tell me why it happened?

2 answers

2

It happens because you are trying to compare the Enum code with the Enum itself, because the getUpdateaction() returns the One in itself, but you try to make the case with the Enum code, thus causing this error, just you remove the enumUpdateAction.NEW.getCodigo() and put enumUpdateAction.NEW.

  • No, no. Events.get(0). getUpdateaction() returns a number from 0 to 4. Therefore, to not put 0, 1, 2, 3 and 4 in the cases, I created an Enum, because the number 0, for example, refers to NEW. So I don’t have to keep remembering what each number refers to. Understand? ENUM makes this part easier but I’m not getting the int that NEW refers to, which is 0. getUpdateAction() has nothing to do with Enum.

1

A value used in a case needs to be a Constant variable, that is, a variable final of a primitive type or String initialized at compile time, or a enum. This is not the case for the value returned by the method getCodigo of his enum.

If the method getUpdateAction return a enum just do this:

Use the name of enum in the case :

switch(Eventos.get(0).getUpdateaction()) {
    case NEW:
        ArrayEventos.add(InserirOrdenado(ArrayEventos, Ordem), Ordem);
        break;

}

In case he returns the code from his enum, change your enum for:

public enum EnumUpdateAction {
    NEW(0), CHANGE(1), DELETE(2), DELETE_THRU(3), DELETE_FROM(4);


    private static final EnumUpdateAction VALUES[] = EnumUpdateAction.values();

    private final int codigo;

    private EnumUpdateAction(int codigo) {
        this.codigo = codigo;
    }

    public int getCodigo() {
        return this.codigo;
    }


    public static final EnumUpdateAction deCodigo(int codigo) {
        for(EnumUpdateAction e : VALUES) {
            if(e.codigo == codigo) {
                return e;
            }
        }
        throw new IllegalArgumentException("codigo inválido: " + codigo);
    }
}

And your switch for:

switch(EnumUpdateAction.deCodigo(Eventos.get(0).getUpdateaction())) {
    case NEW:
        ArrayEventos.add(InserirOrdenado(ArrayEventos, Ordem), Ordem);
        break;

}

Remark: by convention, class names in Java are written in Camelcase.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.