This example demonstrate how to user enum
‘s name()
method to get enum constant name exactly as declared in the enum declaration.
package org.kodejava.basic;
enum ProcessStatus {
IDLE, RUNNING, FAILED, DONE;
@Override
public String toString() {
return "Process Status: " + this.name();
}
}
public class EnumNameDemo {
public static void main(String[] args) {
for (ProcessStatus processStatus : ProcessStatus.values()) {
// Gets the name of this enum constant, exactly as
// declared in its enum declaration.
System.out.println(processStatus.name());
// Here we call to our implementation of the toString
// method to get a more friendly message of the
// enum constant name.
System.out.println(processStatus);
}
}
}
Our program result:
IDLE
Process Status: IDLE
RUNNING
Process Status: RUNNING
FAILED
Process Status: FAILED
DONE
Process Status: DONE
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023