1

Are two dimensional enums a thing in java? i.e.:

public enum Modules {
    ATTACK(enum States{IDLE, NEAREST, NEARESTTOHQ;}),
    MOVE(enum States{IDLE, NORTH, SOUTH, EAST, WEST;}),
    SPAWN(enum States{IDLE, SIMPLESPAWN;});
}
iluvAS
  • 513
  • 4
  • 9

3 Answers3

0

In short, no. As per the JLS:

An enum constant defines an instance of the enum type.

It follows that each enum constant, as it is of the type of the Enum, cannot contain a new nested member declaration. You could define all 3 of your "sub" enums as nested members of the Enum, and then access them like:

Modules.AttackOptions.IDLE

By the same logic (and the JLS allows this) Enum constants can also contain overrides for an abstract or default method, and hold their own "state", like so:

public enum Modules {
    ATTACK(0, 1, 2) { public int getCost() { return 2;} },
    MOVE  (3, 4, 5) {  }, // no method override
    SPAWN (6, 7)    { public int getCost() { return 1000;} };

    private final int[] vars;
    private Modules(int... vars) { this.vars = vars; }
    public int getCost() { return 1; }
    public int[] getVars() { return this.vars; }

    public enum AttackOptions {
        IDLE, NEAREST, SPAWN
    }
}
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
0

I have a little trouble understanding what you are trying to do, but might this do it?

enum States {
    IDLE, NEAREST, NEARESTTOHQ,NORTH,SOUTH,EAST,WEST,IDLESPAWN
}

public enum Modules {
    ATTACK(IDLE, NEAREST, NEARESTTOHQ),
    MOVE(IDLE, NORTH, SOUTH, EAST, WEST),
    SPAWN(IDLE, SIMPLESPAWN);

    private States[] states;

    public Modules(States... states) {
        this.states=states;
    }
}

(Note that to get the syntax I used I believe you will need to put States in it's own file and include an import States.* in Modules)

Bill K
  • 62,186
  • 18
  • 105
  • 157
0

Not in the sense you're thinking, but this can be accomplished more straightforwardly.

If those enums are capable of holding an array of States, you can set their known states at declaration time.

Example:

public enum Modules {
    ATTACK(States.IDLE, States.NEAREST, States.NEARESTTOHQ),
    MOVE(States.IDLE, States.NORTH, States.SOUTH, States.EAST, States.WEST),
    SPAWN(States.IDLE, States.SIMPLESPAWN);

    private final States[] knownStates;

    private Modules(States... knownStates) {
        this.knownStates = knownStates;
    }

    public States[] getKnownStates() {
        return knownStates.clone();
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230