I declare an enum as :
enum Sex {MALE,FEMALE};
And then, iterate enum as shown below :
for(Sex v : Sex.values()){
System.out.println(" values :"+ v);
}
I checked the Java API but can’t find the values() method? I’m curious as to where this method comes from?
API link :
https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html
You can’t see this method in javadoc because it’s added by the compiler.
Documented in three places :
Enum.valueOfclass(The special implicit
valuesmethod is mentioned in description ofvalueOfmethod)The
valuesfunction simply list all values of the enumeration.