An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comm.
Approach: Single enum class
Java
public enum StatusEnum {ACTIVATED, DEACTIVATED, SUSPENDED}
Approach: Multiple enums in a single class
Java
public class EnumClasss {enum STATUS {ACTIVATED, DEACTIVATED, SUSPENDED}enum LOGINTYPE {ADMIN, USER}// print enum valuepublic static void main(String[] args) {for (STATUS evalue : STATUS.values()) {System.out.println(evalue);}}}
Approach: Define Enum with values
Java
enum UserEnum {ACTIVATED(1), DEACTIVATED(3), SUSPENDED(2);private int value;UserEnum(int value) {this.value = value;}public int getValue() {return value;}public void setValue(int value) {this.value = value;}}public class TestEnum {public static void main(String[] args) {// print enum valuefor (UserEnum evalue : UserEnum.values()) {System.out.println(evalue + " " + evalue.getValue());}}}
No comments:
Post a Comment