How do I get name of enum constant?

Category: fundamental, viewed: 734 time(s).

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.example.fundametal;

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 ps : ProcessStatus.values()) {
            //
            // Gets the name of this enum constant, exactly as
            // declared in its enum declaration.
            //
            System.out.println(ps.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(ps.toString());
        }
    }
}

Our program result:

IDLE
Process Status: IDLE
RUNNING
Process Status: RUNNING
FAILED
Process Status: FAILED
DONE
Process Status: DONE
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats