I wrote the below enum from which I need to extract the name and its value:

public enum UserEnum {
    TreeUser("/tree"), ParentUser("/parent");

    private String value;

    UserEnum(String value) {
        this.value = value;

    public String value() {
        return value;

This is the way I am using the above UserEnum in my code base:

// extract TreeUser
String nameOfTree = UserEnum.TreeUser.name();
String valueOfTree = UserEnum.TreeUser.value();

// extract ParentUser   
String nameOfParent = UserEnum.ParentUser.name();
String valueOfParent = UserEnum.ParentUser.value(); 

// and I am using UserEnum this way as well to make name1=value1,name2=value2   
String mapping = UserEnum.TreeUser.name() + "="
                + UserEnum.TreeUser.value() + "," + UserEnum.ParentUser.name() + "="
                + UserEnum.ParentUser.value();  

I am opting for a review to see whether I can simplify anything in my enum.

  • \$\begingroup\$ @Vogel612 This is the actual Enum I have in my code. And those are the ways I am using Enum in my code base. I just need to extract name and value from the Enum. \$\endgroup\$
    – david
    Dec 24, 2014 at 23:51
  • \$\begingroup\$ facepalm I should maybe read up on enums again... I had getName() in the back of my head and was expecting that instead. Thank you for clarifying \$\endgroup\$
    – Vogel612
    Dec 24, 2014 at 23:54
  • \$\begingroup\$ You do realize that the name() of an Enum is literally the name used for the values, i.e. TreeUser.name().equals("TreeUser"); // true and ParentUser.name().equals("ParentUser"); // true ? \$\endgroup\$
    – h.j.k.
    Dec 26, 2014 at 7:43
  • \$\begingroup\$ @h.j.k. Yes I do realize that? Is there anything wrong I did in my above usages? \$\endgroup\$
    – david
    Dec 26, 2014 at 17:53
  • \$\begingroup\$ @user2809564 I see you have accepted @thepace's answer to use toString(), that's what I'll recommend too. :) \$\endgroup\$
    – h.j.k.
    Dec 28, 2014 at 12:37

2 Answers 2


Not sure about the usage of nameOfTree , nameOfParent and same for the value. But one thing to simplify your mapping and provide a representation for your enum is:

public String toString(){
    return  this.name() + "=" + this.value()

// and mapping cna be written as.
String mapping = UserEnum.TreeUser.toString() +  "," + UserEnum.ParentUser.toString();
String mapping = UserEnum.TreeUser +  "," + UserEnum.ParentUser; // toString() is called implictly.

You seem to be looking for something completely different.

Currently you are putting settings into your code... hardcoded.
You should try not to mix up Logic and Data. It might be very favorable to instead use something like a properties-file, which follows the same syntax as your mapping:

Properties userDirectories = new Properties();
for (String user : userDirectories.propertyNames()) {
    String dir = userDirectories.get(user);
     //use this instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.