79

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!

8
  • 8
    The methods shouldn't be in all caps. Commented Oct 25, 2013 at 23:02
  • Not even for enums? Normally I use camel case, but I thought when referencing something static that never gets changed it should be all caps much like the enum itself would be. Commented Oct 25, 2013 at 23:04
  • 7
    It's also better if you actually had fields like 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. Commented Oct 25, 2013 at 23:05
  • 1
    @SonofLysander Methods themselves should always be camel case. I've only never seen all caps for enums and constants (like for example, Math.PI). Commented Oct 25, 2013 at 23:09
  • 1
    (I've also seen all caps for 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")) Commented Oct 25, 2013 at 23:17

2 Answers 2

171

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;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Marcin why shouldn't that be private?
enum constructors are implicitly private
How to call the method. What would States.getAbbreviatedName() will print in sysout?
we can use Status.valueOf("MASSACHUSETTS").getAbbreviatedName();, Status.valueOf("MASSACHUSETTS").isOriginalColony();, etc
How to have a default value in this case in-case enum does not match?
|
-1

This is a filtered value from list

List<String> valueList= Stream.of(States.values())
               .filter(m->m.ORIGINAL_COLONY()==true)
                .map(m->m.ABBR())
                .collect(Collectors.toList());
    enter code here



public enum States {
    MASSACHUSETTS("Massachusetts",  "MA",   true),
    CHICAGO("Chicago",  "MA",   true),
    TROUT("trout",  "MA",   true),
    MICHIGAN("Michigan",            "MI",   false);
    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];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.