The values in my enum are words that need to have spaces in them, but enums can’t have spaces in their values so it’s all bunched up. I want to override toString() to add these spaces where I tell it to.
I also want the enum to provide the correct enum when I use valueOf() on the same string that I added the spaces to.
For example:
public enum RandomEnum
{
StartHere,
StopHere
}
Call toString() on RandomEnum whose value is StartHere returns string "Start Here". Call valueof() on that same string ("Start Here") returns enum value StartHere.
How can I do this?
You can try out this code. Since you cannot override
valueOfmethod you have to define a custom method (getEnumin the sample code below) which returns the value that you need and change your client to use this method instead.