Results 1 to 17 of 17
- 01-25-2012, 08:21 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Question about enums and for loop
Java Code:public enum FRIENDS { Norm, Troll, DarrylBurke, }So I have a reference f of Type f, and it iterates in FRIENDS.values() enum, and prints out:Java Code:for(FRIENDS f: FRIENDS.values()) { System.out.println(f); }
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 ?
- 01-25-2012, 08:23 PM #2
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."How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-25-2012, 08:24 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question about enums and for loop
What does that mean, automatically adds some special methods ?
Is it like an invisible method ?
- 01-25-2012, 08:33 PM #4
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?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-25-2012, 08:34 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question about enums and for loop
I am just trying to learn.
- 01-25-2012, 08:54 PM #6
Re: Question about enums and for loop
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-25-2012, 09:12 PM #7
Re: Question about enums and for loop
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.... it's just compiler magic.
Classes
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-25-2012, 09:29 PM #8
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question about enums and for loop
there is no .values() method in java.lang.Enum
- 01-25-2012, 10:18 PM #9
Re: Question about enums and for loop
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-26-2012, 03:06 AM #10
- 01-26-2012, 07:31 AM #11
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question about enums and for loop
So it is magic then ?
- 01-26-2012, 08:49 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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:"
Java 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);
- 01-26-2012, 10:14 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Question about enums and for loop
"Troll"??
;)
- 01-26-2012, 10:15 AM #14
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
- 01-26-2012, 11:30 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Question about enums and for loop
It happens a lot...:)
- 01-26-2012, 01:16 PM #16
Re: Question about enums and for loop
And I thought that was meant as a prefix to the next enum constant ;)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-26-2012, 01:17 PM #17
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
General question, why don't Java classes use enums?
By Anza Power in forum Advanced JavaReplies: 9Last Post: 12-22-2011, 05:50 AM -
Copy construtor with enums -
By danthegreat in forum New To JavaReplies: 1Last Post: 11-18-2011, 12:34 AM -
Enums taking in enums?
By rizowski in forum New To JavaReplies: 7Last Post: 06-11-2011, 01:40 PM -
declaring problems with enums
By jackrulesok in forum New To JavaReplies: 10Last Post: 04-30-2010, 10:16 AM -
why we are using enums in Java?
By manish.anchan in forum New To JavaReplies: 7Last Post: 01-08-2010, 04:41 PM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks