w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Java Enum toString() method

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).





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.