public enum EnumSample1 {
ITEM_A,
ITEM_B
}
The enum keyword tells the compiler that this is a special construct that needs to be translated into more complex code.
public final class EnumSample1 extends java.lang.Enum<EnumSample1> {
public static final EnumSample1 ITEM_A = new EnumSample1("ITEM_A", 0);
public static final EnumSample1 ITEM_B = new EnumSample1("ITEM_B", 1);
private static final EnumSample1[] $VALUES = {ITEM_A, ITEM_B};
private EnumSample1(String name, int ordinal) {
super(name, ordinal);
}
public static EnumSample1[] values() {
return $VALUES.clone();
}
public static EnumSample1 valueOf(String name) {
return Enum.valueOf(EnumSample1.class, name);
}
}
Notes:
package com.mtitek.enums;
enum EnumSample1 {
ITEM_A, ITEM_B
}
public class EnumSample1Test {
public static void main(String[] args) {
EnumSample1 myEnumSample1 = EnumSample1.ITEM_B;
// Using switch statement
switch(myEnumSample1) {
case ITEM_A:
System.out.println(EnumSample1.ITEM_A.name() + " : " + EnumSample1.ITEM_A.ordinal());
break;
case ITEM_B:
System.out.println(EnumSample1.ITEM_B.name() + " : " + EnumSample1.ITEM_B.ordinal());
break;
}
// Using equals() method
if(myEnumSample1.equals(EnumSample1.ITEM_A)) {
System.out.println(EnumSample1.ITEM_A.name() + " : " + EnumSample1.ITEM_A.ordinal());
} else if(myEnumSample1.equals(EnumSample1.ITEM_B)) {
System.out.println(EnumSample1.ITEM_B.name() + " : " + EnumSample1.ITEM_B.ordinal());
}
// Using == operator - preferred for enums (more efficient and null-safe)
if(myEnumSample1 == EnumSample1.ITEM_A) {
System.out.println(EnumSample1.ITEM_A.name() + " : " + EnumSample1.ITEM_A.ordinal());
} else if(myEnumSample1 == EnumSample1.ITEM_B) {
System.out.println(EnumSample1.ITEM_B.name() + " : " + EnumSample1.ITEM_B.ordinal());
}
System.out.println("All enum values:");
for(EnumSample1 value : EnumSample1.values()) {
System.out.println(" " + value.name() + " (ordinal: " + value.ordinal() + ")");
}
EnumSample1 enumSample1 = EnumSample1.valueOf("ITEM_A");
System.out.println("Enum: " + enumSample1);
}
}
Output:ITEM_B : 1 ITEM_B : 1 ITEM_B : 1 All enum values: ITEM_A (ordinal: 0) ITEM_B (ordinal: 1) Enum: ITEM_ANotes:
package com.mtitek.enums;
enum EnumSample2 {
ITEM_A, // Uses default constructor
ITEM_B(), // Explicitly calls default constructor
ITEM_C(100); // Uses parameterized constructor
// Default constructor for the enum
EnumSample2() {
this(0);
}
// Parameterized constructor for the enum
EnumSample2(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
// Instance field
private Integer value;
// Static field
public static final Integer ENUM_ID = 99999;
}
public class EnumSample2Test {
public static void main(String[] args) {
System.out.println(EnumSample2.ITEM_A.name() + " : " + EnumSample2.ITEM_A.ordinal() + " : " + EnumSample2.ITEM_A.getValue());
System.out.println(EnumSample2.ITEM_B.name() + " : " + EnumSample2.ITEM_B.ordinal() + " : " + EnumSample2.ITEM_B.getValue());
System.out.println(EnumSample2.ITEM_C.name() + " : " + EnumSample2.ITEM_C.ordinal() + " : " + EnumSample2.ITEM_C.getValue());
System.out.println("ENUM_ID : " + EnumSample2.ENUM_ID);
// Enums are mutable - not recommended
EnumSample2.ITEM_A.setValue(1000);
System.out.println("Modified ITEM_A value: " + EnumSample2.ITEM_A.getValue());
}
}
Output:ITEM_A : 0 : 0 ITEM_B : 1 : 0 ITEM_C : 2 : 100 ENUM_ID : 99999 Modified ITEM_A value: 1000Notes:
package com.mtitek.enums;
enum EnumSample3 {
ITEM_A {
// Constant-specific field
private final int localValue = 10;
@Override
public String getGlobalToString() {
return name() + " : " + ordinal() + " : Global (localValue=" + localValue + ")";
}
// Constant-specific method
public int getLocalValue() {
return localValue;
}
},
ITEM_B {
@Override
public String getGlobalToString() {
return name() + " : " + ordinal() + " : Global : " + getLocalToString();
}
// This method is local and visible only inside ITEM_B
private String getLocalToString() {
return "Local";
}
};
// Abstract method that must be implemented by all constants
public abstract String getGlobalToString();
// Regular method available to all constants
public String getDescription() {
return "This is " + name() + " with ordinal " + ordinal();
}
}
public class EnumSample3Test {
public static void main(String[] args) {
System.out.println(EnumSample3.ITEM_A.getGlobalToString());
System.out.println(EnumSample3.ITEM_B.getGlobalToString());
// Using the regular method
for (EnumSample3 item : EnumSample3.values()) {
System.out.println(item.getDescription());
}
// Constant-specific methods are not accessible from outside
// System.out.println(EnumSample3.ITEM_A.getLocalValue()); // Compiler error: The method getLocalValue() is undefined for the type EnumSample3
}
}
Output:ITEM_A : 0 : Global (localValue=10) ITEM_B : 1 : Global : Local This is ITEM_A with ordinal 0 This is ITEM_B with ordinal 1Notes: