Basically what I've done is write an enum for States, and I want to not only be able to access them just as states but also access their abbreviation and whether or not they were an original colony.
public enum States {
...
MASSACHUSETTS("Massachusetts", "MA", true),
MICHIGAN("Michigan", "MI", false),
...; //so on and so forth for all fifty states
private final Object[] values;
States(Object... vals) {
values = vals;
}
public String FULL() {
return (String) values[0];
}
public String ABBR() {
return (String) values[1];
}
public boolean ORIGINAL_COLONY(){
return (boolean) values[2];
}
}
This seems to work as I'd expect it to. I can
System.out.println(States.ALABAMA); // Prints "ALABAMA"
System.out.println(States.ALABAMA.FULL()); // Prints "Alabama"
System.out.println(States.ALABAMA.ABBR()); // Prints "AL"
System.out.println(States.ALABAMA.ORIGINAL_COLONY());// Prints "false"
For this particular scenario involving enums, is this the best way to do this or is there a better way to setup and format this enum? Thanks to all in advance!
name
,abbreviation
,isOriginalColony
and had a constructor taking(String, String, boolean)
. That way, it's more readable, and also helps prevent someone adding in another state with the arguments in the wrong order.Math.PI
).static final
references that aren't compile-time constants, though I personally don't like that. For instance,public static final FOO = System.getProperty("foo")
)