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 "A"
which would be Blah.A
. How would it be possible to do this?
Is the Enum.valueOf()
the method I need? If so, how would I use this?
Best Answer
Yes,
Blah.valueOf("A")
will give youBlah.A
.Note that the name must be an exact match, including case:
Blah.valueOf("a")
andBlah.valueOf("A ")
both throw anIllegalArgumentException
.The static methods
valueOf()
andvalues()
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.