How to nest an Enum inside the value of an Enum

Posted by Mathieu on Stack Overflow See other posts from Stack Overflow or by Mathieu
Published on 2010-04-14T11:19:12Z Indexed on 2010/04/14 11:23 UTC
Read the original article Hit count: 562

Filed under:
|
|

I'd like to know if it is possible in Java to nest Enums.

Here what i'd like to be able to do :

Have an enum Species made of CAT and DOG wich would grant me access to sub enums of the available CAT and DOG breeds. For example, i'd like to be able to test if wether a CAT or a DOG and if an animal is a PERSAN CAT or a PITBULL DOG. CAT and DOG breeds must be distinct enums ,i.e a CatBreeds enum and a DogBreeds enum.

Here is an example of access pattern i'd like to use :

Species :

  • Species.CAT
  • Species.DOG

Breeds :

  • Species.CAT.breeds.PERSAN
  • Species.DOG.breeds.PITBULL

© Stack Overflow or respective owner

Related posts about java

Related posts about enums