Question about enums and for loop
Code:
public enum FRIENDS
{
Norm,
Troll,
DarrylBurke,
}
Code:
for(FRIENDS f: FRIENDS.values())
{
System.out.println(f);
}
So I have a reference f of Type f, and it iterates in FRIENDS.values() enum, and prints out:
Norm
Troll
DarrylBurke
But in API, for enum class, I do not see any .values() method?
Enum (Java Platform SE 6)
So what exactly is FRIENDS.values() give me here ? An array ?
Re: Question about enums and for loop
From the tutorial: Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
"The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared."
Re: Question about enums and for loop
What does that mean, automatically adds some special methods ?
Is it like an invisible method ?
Re: Question about enums and for loop
I'm not really sure why it bothers you? There are a few things in the language like this. For example, where does the length member of an array come from?
Re: Question about enums and for loop
I am just trying to learn.
Re: Question about enums and for loop
Quote:
Originally Posted by
fatabass
I am just trying to learn.
Fair enough. But in this case, it's just compiler magic.
Re: Question about enums and for loop
Quote:
... it's just compiler magic.
Not really. All enums implicitly extend java.lang.Enum, just as classes without an extends... clause implicitly extend java.lang.Object. The rest is normal inheritance.
Classes
db
Re: Question about enums and for loop
there is no .values() method in java.lang.Enum
Re: Question about enums and for loop
Quote:
Originally Posted by
DarrylBurke
Not really. All enums implicitly extend java.lang.Enum, just as classes without an extends... clause implicitly extend java.lang.Object. The rest is normal inheritance.
Classes
db
Sure, that's where it gets the rest of the functions. But the .values() function is added by compiler magic.
Re: Question about enums and for loop
Quote:
Originally Posted by
KevinWorkman
Sure, that's where it gets the rest of the functions. But the .values() function is added by compiler magic.
Ah, right. Sorry I missed that.
db
Re: Question about enums and for loop
Re: Question about enums and for loop
Yes: you get the method but it is not explicitly declared anywhere.
The magic is documented however! In the JLS 8.9 Enums we have " if E is the name of an enum type, then that type has the following implicitly declared static methods:"
Code:
/**
* Returns an array containing the constants of this enum
* type, in the order they're declared. This method may be
* used to iterate over the constants as follows:
*
* for(E c : E.values())
* System.out.println(c);
*
* @return an array containing the constants of this enum
* type, in the order they're declared
*/
public static E[] values();
/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);
Re: Question about enums and for loop
Re: Question about enums and for loop
Quote:
Originally Posted by
Tolls
"Troll"??
;)
Oh man! Sorry! Seriously no intention. :)
Tolls of course..
Re: Question about enums and for loop
Re: Question about enums and for loop
And I thought that was meant as a prefix to the next enum constant ;)
db
Re: Question about enums and for loop
:) Definitively not..
Seriously some sort of mistake I can't find an excuse for... Surfing the Internet too much obviously damaged my brain.