1

In my application I have an enum like that:

public enum EAnimals {

    dogs(1, "white", "big"),
    cats(2, "black", "small");

    private Integer animalId;
    private String color;
    private String size;

    EAnimals(Integer animalId, String color, String size) {
        this.animalId = animalId;
        this.color = color;
        this.size = size;
    }

    public Integer getAnimalId() {
        return animalId;
    }
    public String getColor() {
        return color;
    }
    public String getSize() {
        return size;
    }

What I`m trying to achieve here is to get animal ID (1,2,..) from Managed Bean using switch case:

public AnimalsId getDynamicAnimalId() {
    switch (animalId) {
    case EAnimals.dogs.getAnimalId():
        size ="small";
        return size;

    case EAnimals.cats.getAnimalId():
        size = "big";
        return size;
    default:
        return "Error";
    }
}

In switch statement "case EAnimals.dogs.getAnimalId():" doesn't work for me and I can`t figure out how to do it.

EDIT: Corrected few mistakes which I made , when I was rewriting my code from IDE to stackoverflow. I don`t have this errors in my actual code.

  • You are trying to create an actual `class` of an `enum`. By default, an element of an enum get's a number assigned automatically. See [the documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) – pietv8x Nov 30 '14 at 09:05
  • Chthonic Project, not exactly. I have a different kind of problem here. – Raimonds Sokoļvjaks Nov 30 '14 at 09:06
  • 1
    It looks like `size` is a `String` and you aren't using the switch statement on an `Enum` but rather on an `Integer`. – Jared Rummler Nov 30 '14 at 09:08
  • I voted to close it, because there are so many errors and misunderstandings here, the answer would be too broad (and boil down to: "try and learn Java") – fdreger Nov 30 '14 at 20:28

1 Answers1

1

First, I see a few errors in your enum. You need a comma (not a semicolon). Your have two fields that are String(s) but you've declared them to return Integer.

public enum EAnimals {
    dogs(1, "white", "big"), // <-- ,
    cats(2, "black", "small");
    private Integer animalId;
    private String color;
    private String size;

    EAnimals(Integer animalId, String color, String size) {
        this.animalId = animalId;
        this.color = color;
        this.size = size;
    }

    public Integer getAnimalId() {
        return animalId;
    }

    public String getColor() { // <-- String
        return color;
    }

    public String getSize() { // <-- using an enum in a switch.
        switch (this) {
        case dogs:
            return "small";
        case cats:
            return "big";
        default:
            return "Error";
        }
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I don't understand, what's wrong with `return size`? –  Nov 30 '14 at 09:12
  • @RC Nothing, you asked how to switch on an `enum`. You might say `case EAnimals.dogs:` not `case EAnimals.dogs.getAnimalId():`. In the above example, you could remove `size`. – Elliott Frisch Nov 30 '14 at 09:15
  • 1
    Oh! it's an example. (btw +1 for the comma/semicolon) –  Nov 30 '14 at 09:19