This example shows how to create and use an Enum in Java. Sample code to create user defined constants with enums type special class.
— In Java (from 1.5) an Enum is a special data type that enables for a variable to be a set of predefined constants.
— Keyword ‘enum’ is used to define an Enum.
— Because they are constants, the names of an enum type’s fields are in uppercase letters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public enum UserStatus { PENDING("P"), ACTIVE("A"), INACTIVE("I"), DELETED("D"); private String statusCode; private UserStatus(String s) { statusCode = s; } public String getStatusCode() { return statusCode; } } public class EnumTest { public static void main(String[] args) { System.out.println(UserStatus.ACTIVE.getStatusCode()); } } |