Results 1 to 17 of 17
Like Tree4Likes
  • 1 Post By DarrylBurke
  • 1 Post By KevinWorkman
  • 2 Post By pbrockway2

Thread: Question about enums and for loop

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Question about enums and for loop

    Java Code:
    public enum FRIENDS 
    {
    	Norm,
    	Troll,
    	DarrylBurke,
    }
    Java 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 ?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default 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!

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question about enums and for loop

    What does that mean, automatically adds some special methods ?

    Is it like an invisible method ?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default 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!

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question about enums and for loop

    I am just trying to learn.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Question about enums and for loop

    Quote Originally Posted by fatabass View Post
    I am just trying to learn.
    Fair enough. But in this case, it's just compiler magic.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Question about enums and for loop

    ... 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
    diamonddragon likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question about enums and for loop

    there is no .values() method in java.lang.Enum

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Question about enums and for loop

    Quote Originally Posted by DarrylBurke View Post
    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.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Question about enums and for loop

    Quote Originally Posted by KevinWorkman View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question about enums and for loop

    So it is magic then ?

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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);
    JosAH and KevinWorkman like this.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Question about enums and for loop

    "Troll"??

    ;)

  14. #14
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question about enums and for loop

    Quote Originally Posted by Tolls View Post
    "Troll"??

    ;)
    Oh man! Sorry! Seriously no intention. :)

    Tolls of course..

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Question about enums and for loop

    It happens a lot...:)

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Question about enums and for loop

    And I thought that was meant as a prefix to the next enum constant ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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.

Similar Threads

  1. General question, why don't Java classes use enums?
    By Anza Power in forum Advanced Java
    Replies: 9
    Last Post: 12-22-2011, 05:50 AM
  2. Copy construtor with enums -
    By danthegreat in forum New To Java
    Replies: 1
    Last Post: 11-18-2011, 12:34 AM
  3. Enums taking in enums?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 06-11-2011, 01:40 PM
  4. declaring problems with enums
    By jackrulesok in forum New To Java
    Replies: 10
    Last Post: 04-30-2010, 10:16 AM
  5. why we are using enums in Java?
    By manish.anchan in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •