Are there enums nested in Java?

3

I want to make an enum of countries to which you can access their states, something similar to this:

public enum SomeEnum {

     ARGENTINA {
       BUENOS_AIRES;
     }

     UNITED_STATES {
       CALIFORNIA, FLORIDA, NEW_YORK, ALASKA;
     }

}

SomeEnum state1 = SomeEnum.ARGENTINA.BUENOS_AIRES
SomeEnum state2 = SomeEnum.UNITED_STATES.CALIFORNIA;
    
asked by user15796 05.10.2016 в 15:07
source

1 answer

3

You can do the same if instead of the enum you have called "SomeEnum", you use a class with static members.

package pruebas;

class PAIS {
    static enum ARGENTINA{BUENOS_AIRES};
    static enum UNITED_STATES { CALIFORNIA, FLORIDA, NEW_YORK, ALASKA; }
}

public class Pruebas {
    public static void main(String[] args) {
        Enum estado1 = PAIS.ARGENTINA.BUENOS_AIRES;
        Enum estado2 = PAIS.UNITED_STATES.ALASKA;
    }
}
    
answered by 05.10.2016 в 16:20