Tag Archives: enum

Enum with String values implementation in Java

enum_data_type
This is classical realization of enum with string values:

    private enum SomeTestEnum {

        ITEM1("TheStringNameOfItem1"),
        ITEM2("TheStringNameOfItem2"),
        ITEM3("TheStringNameOfItem3");

        private final String name;

        /**
         * Default constructor
         */
        SomeTestEnum(String name) {
            this.name = name;
        }

        /**
         * Getter for current instance name value
         */
        public String getName() {
            return name;
        }

        public static SomeTestEnum getByName(String name) {
            for (SomeTestEnum item : SomeTestEnum.values()) {
                if (item.getName().equals(name)) {
                    return item;
                }
            }
            return null;
        }
    }