Yes Enum is a class in Java:
public enum Animal { ELEPHANT(true), GIRAFFE(true), TURTLE(false), SNAKE(false), FROG(false); private final boolean mammal; private Animal(final boolean mammal) { this.mammal = mammal; } public boolean isMammal() { return this.mammal; } }
but in your case, for a real system, I would do it Enum, since there is a fixed set of types of animals.
public enum Type { AMPHIBIAN, MAMMAL, REPTILE, BIRD } public enum Animal { ELEPHANT(Type.MAMMAL), GIRAFFE(Type.MAMMAL), TURTLE(Type.REPTILE), SNAKE(Type.REPTILE), FROG(Type.AMPHIBIAN); private final Type type; private Animal(final Type type) { this.type = type; } public boolean isMammal() { return this.type == Type.MAMMAL; } public boolean isAmphibian() { return this.type == Type.AMPHIBIAN; } public boolean isReptile() { return this.type == Type.REPTILE; }
Also note that it is also important to make any instance variable final .
Jarrod Roberson Mar 16 2018-10-18T00-03-16 18:36
source share