java Enum valueOf has two parameters?

As you indicated in previous comments that you find the text in the documentation confusing, and since your profile indicates you are a novice programmer:

Enum is the superclass of all enums you will declare. In your example, WorkDays can be seen as a specific case of the Enum class. The valueOf() static method documentation is writen for this parent Enum class. Meaning that in your case, it would be called as: Enum.valueOf(WorkDays.class, "MONDAY").

Now, since you made your own Enum (i.e. WorkDays), you don't need to use this static parent method. You can just use the method that is exposed by your self-created enum.

WorkDays.valueOf("Monday")

This is "implicitly declared" meaning that there it will be there for every one of your self-created enums.


The snippet you shared uses the implicitly declared method referenced in the second paragraph:

Note that for a particular enum type T, the implicitly declared public static T valueOf(String) method on that enum may be used instead of this method to map from a name to the corresponding enum constant.

The first paragraph refers to calling the method via the Enum class:

System.out.println(Enum.valueOf(WorkDays.class, "MONDAY"));

Tags:

Java

Enums