Can I add a function to enums in Java?

Posted by Samuel Carrijo on Stack Overflow See other posts from Stack Overflow or by Samuel Carrijo
Published on 2010-03-16T18:32:30Z Indexed on 2010/03/16 18:41 UTC
Read the original article Hit count: 418

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about enum