Can I add a function to enums in Java?
- by Samuel Carrijo
Hi,
I have an enum, which looks like
public enum Animal {
ELEPHANT,
GIRAFFE,
TURTLE,
SNAKE,
FROG
}
and I want to do something like
Animal frog = ANIMAL.FROG;
Animal snake = ANIMAL.SNAKE;
boolean isFrogAmphibian = frog.isAmphibian(); //true
boolean isSnakeAmphibian = snake.isAmphibian(); //false
boolean isFrogReptile = frog.isReptile(); //false
boolean isSnakeReptile = snake.isReptile(); //true
boolean isFrogMammal = frog.isMammal(); //false
boolean isSnakeMammal = snake.isMammal(); //false
Can I do it in Java?