Malachi Malachi -5 years ago 125
Java Question

Lookup enum by string value

Say I have an enum which is just

public enum Blah {
A, B, C, D

and I would like to find the enum value of a string, for example
which would be
. How would it be possible to do this?

Is the
the method I need? If so, how would I use this?

Answer Source

Yes, Blah.valueOf("A") will give you Blah.A.

The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType shows both methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download