3

I have several Java enums using underscores to separate words. In each of my enums I have a toString method which is implemented as super.toString().replace("_", " ") so that I can display the String values on a GUI without the underscores. Is there some way I can have all my enums use this toString method without duplicating it across all of them? If this were a regular class the answer is simple, implement toString() then have all my classes that need that implementation extend that class, but enums can't extend anything, not even another enum.

public enum Example1 {
    HELLO_WORLD, HELLO_EARTH, HELLO_GLOBE
    @Override public String toString() {
        return super.toString().replace("_", " ");
    }
}
public enum Example2 {
    ANSWER_1, ANSWER_2, ANSWER_3
    @Override public String toString() {
        return super.toString().replace("_", " ");
    }
}

Does anyone have a solution that doesn't duplicate this toString() method across all enums?

4
  • 1
    Use a helper method when printing your enums. Commented Feb 27, 2014 at 16:29
  • Is it necessary to use "enum"? Or can you change your implementation to type safe enums so you can do some inheritance? Commented Feb 27, 2014 at 16:39
  • @Neel I see your point, forgetting the Java enum and use the method from before Java actually recognized enums. That's a good idea. Commented Feb 27, 2014 at 16:41
  • updated with typesafe enum guideline... you might need to fill in the gaps. Commented Feb 27, 2014 at 16:49

3 Answers 3

5

In Java 8 we can pull in methods using interfaces so you can now actually add functionality to enums through a form of inheritance.

public interface HasName {
    public String name();
}

public interface PoliteEnum extends HasName {
    default String politeName() {
        return name().replace("_", " ");
    }
}

public enum X implements PoliteEnum {
    A_For_Ism, B_For_Mutton, C_Forth_Highlanders;
}

public void test() {
    for (X x : X.values()) {
        System.out.println(x.politeName());
    }
}

prints

A For Ism
B For Mutton
C Forth Highlanders

I accept that it is not as neat as overriding toString because you cannot override an Object method in the default but it certainly leaves your enum tidy.

Sign up to request clarification or add additional context in comments.

Comments

1
public static String toString(Enum<?> enumVal) {
    return String.valueOf(enumVal).replace("_", " ");
}

Usage:

String asString = toString(HELLO_WORLD);

or,

String asString = toString(ANSWER_1);

Typesafe approach:

public abstract class MyEnum<E extends MyEnum<E>> {

    protected MyEnum(String name) {
        this.name = name;
    }

    private String name;

    protected String name() {
        return name;
    };

    @Override
    public final String toString() {
        return name().replace("_", " ");
    }
}

public final static class Example1 extends MyEnum<Example1> {
    public static final Example1 HELLO_WORLD = new Example1("HELLO_WORLD");

    protected Example1(String name) {
        super(name);
    }
}

Usage:

System.out.println(Example1.HELLO_WORLD);

Comments

1

Declare some helper method like

public static <E extends Enum<E>> String toString(Enum<E> value) {
    return value.name().replace("_", ""); // use name() as suggested in comments
}

Whenever you want to print an enum, use it

String valueToPrint = YourHelperClass.toString(Example2.ANSWER_1);

In Java 8, you could use an interface with a default implementation which all your enums implement (but don't override).

4 Comments

Why would I need E? This helper method should work for any Enum, no?
@Sotirios I though of this too, but in that case my team and I would have to remember to use the helper class (I know, I know). The simplicity of "The answer is: " + enumInstance; automatically calls toString() for me and is already scattered throughout my code.
You can replace .toString() with .name() in this example since by default, this is what an Enum does
@Flappjakk You don't need E, you can use a wildcard (I just try to avoid them). Java 8 will be here soon for the best solution.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.