Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java enum with multiple value types

Tags:

java

enums

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!

like image 836
Paul Nelson Baker Avatar asked Oct 25 '13 23:10

Paul Nelson Baker


People also ask

Can enum have 2 values?

The Enum constructor can accept multiple values.

How do you create multiple enums in Java?

String[] digit = {"one", "two", "three"}; String[] teen= {"ten", "twenty", "thirty"}; String[] anchors = {"hundred", "thousand", "million"}; I'm thinking of transferring these to enums separately, so I will have 3 enum classes: digit , teen and anchors with getValue methods implemented.

Can enums have user defined methods?

Below code uses enums with defined methods: We should define methods as abstract methods and then we have to implement defferent flavours/logic based on each enum members. Because of declaring abstract method at the enum level; all of the enum members require to implement the method.

Can you add methods to enum Java?

Enum Class in JavaAn enum class can include methods and fields just like regular classes. When we create an enum class, the compiler will create instances (objects) of each enum constants.


1 Answers

First, the enum methods shouldn't be in all caps. They are methods just like other methods, with the same naming convention.

Second, what you are doing is not the best possible way to set up your enum. Instead of using an array of values for the values, you should use separate variables for each value. You can then implement the constructor like you would any other class.

Here's how you should do it with all the suggestions above:

public enum States {     ...     MASSACHUSETTS("Massachusetts",  "MA",   true),     MICHIGAN     ("Michigan",       "MI",   false),     ...; // all 50 of those      private final String full;     private final String abbr;     private final boolean originalColony;      private States(String full, String abbr, boolean originalColony) {         this.full = full;         this.abbr = abbr;         this.originalColony = originalColony;     }      public String getFullName() {         return full;     }      public String getAbbreviatedName() {         return abbr;     }      public boolean isOriginalColony(){         return originalColony;     } } 
like image 199
tbodt Avatar answered Sep 20 '22 06:09

tbodt