Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: enum, value of: Why is this code line structured the way it is?

  1. #1
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Cool enum, value of: Why is this code line structured the way it is?

    Dear All:

    Simple program, creates enum with breakfast theme. Then, at random, displays an item.

    Why do I want this program to do this?
    To understand. No real programming application at this juncture.

    Question
    Why is the statement
    BreakfastItem breaky = BreakfastItem.values()[ r.nextInt(8)];
    The way it is?

    I believe that we are creating an object called "breaky", of the class "BreakfastItem".
    I am not sure what value ".Values()" is getting, and I have no idea why the square brackets "[]" are there.

    I AM NOT INTERESTED IN.
    I have some understanding that I am dealing with the .ordinal() value. I know that the ordinal value exists and it corresponds to an enum object of the class "BreakfastItem" Please do not concentrate on the fact that the method ".ordinal()" exists.

    So I understand what the outcome of the line is I am more interested in the STRUCTURE and the why of the structure.

    FOLLOW UP QUESTIONS
    Why is the statement constructed in the way it is? (I don't understand, on the Right hand side, anything after "BreakfastItem.")
    What is with the [], the square brackets are reminiscent of arrays. I have some small knowledge of Arrays.

    FULL CODE OF PROGRAM
     
    public class BreakChoice
    {
     
    enum BreakfastItem 
     
    {
    Eggs, Sauseges, Toast, Pancakes, Tater, Totts, French,
     Porrage
    }							
    public static void main(String[] args)
    {
    Random r = new Random();
    BreakfastItem breaky = BreakfastItem.values()[ r.nextInt(8)];   // LOOK HERE , LOOK HERE :-h
     
    System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky)));
    breaky = BreakfastItem.values()[ r.nextInt(8)];
    System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky)));
    breaky = BreakfastItem.values()[ r.nextInt(8)];
    System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky)));
    breaky = BreakfastItem.values()[ r.nextInt(8)];
    System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky)));
    breaky = BreakfastItem.values()[ r.nextInt(8)];
    System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky)));
    breaky = BreakfastItem.values()[ r.nextInt(8)];
     
    }}

    Thanks all for your help.

    SPACE MONKEY
    Last edited by SPACE MONKEY; March 4th, 2011 at 01:41 PM. Reason: Make it clearer and fix my mistakes.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: enum, value of: Why is this code line structured the way it is?

    This is a good question. There is some wonky syntax and little-known methods going on here.

    First off, keep in mind that BreakfastItem is an enum- more accurately, an Enum type. Enum types have a static method, named values. That method returns an array of all of the Enum constants inside of that Enum type. Follow me?

    So when you call BreakfastItem.values(), you get back an array. So you could actually do:

    BreakfastItem[] valuesArray = BreakfastItem.values();

    But you don't have to instantiate an array variable in order to use the [] brackets to access its items. You can use them directly on the method. It's sort of like you don't have to put an Object in a variable before calling methods on it; you can just call methods directly on whatever is returned from a method. The array brackets act the same way.

    So your line:

    BreakfastItem breaky = BreakfastItem.values()[ r.nextInt(8)];

    Is pretty much doing this:

    BreakfastItem[] valuesArray = BreakfastItem.values();
    int i = r.nextInt(8);
    BreakfastItem breaky = valuesArray[i];

    Hope that helps.

    Recommended reading:
    Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following 3 Users Say Thank You to KevinWorkman For This Useful Post:

    Json (March 4th, 2011), SPACE MONKEY (March 4th, 2011), sunde (March 26th, 2011)

  4. #3
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: enum, value of: Why is this code line structured the way it is?

    Going to chew on this one for a wile. Thanks for your help.
    My brain in hurting though.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: enum, value of: Why is this code line structured the way it is?

    Quote Originally Posted by SPACE MONKEY View Post
    Going to chew on this one for a wile. Thanks for your help.
    My brain in hurting though.
    No problem. Is there anything about my explanation that wasn't clear?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Talking Re: enum, value of: Why is this code line structured the way it is?

    Dear WorkmanKevin:

    So I do have a question, or rather I want to check if my understanding is correct.

    From your line.

    But you don't have to instantiate an array variable in order to use the [] brackets to access its items.

    I think you are saying:

    You do not have to create an array to use the “[]” to access the enum/psudoArrays items stored in the array, using the method ".Values()".

    Is that correct.

    Additionally I am very interested in the function of "[]" over "()" in enums and arrays.

    The "()" are used to pass arguments/values to methods.

    What are the "[]" for?

    From what I have seen, with arrays, static methods are expressed within the "[]".

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: enum, value of: Why is this code line structured the way it is?

    Quote Originally Posted by SPACE MONKEY View Post
    You do not have to create an array to use the “[]” to access the enum/psudoArrays items stored in the array, using the method ".Values()".
    Sort of. You do not have to store the reference to the array in order to use the brackets on it. You can use a call to the .values() method anywhere you would use an array variable (other than assignment, obviously). So you can use the [] directly on the value returned by the .values() method- which is a reference to an array.


    Quote Originally Posted by SPACE MONKEY View Post
    Additionally I am very interested in the function of "[]" over "()" in enums and arrays.

    The "()" are used to pass arguments/values to methods.

    What are the "[]" for?
    The [] are always array index accessors. The () are used with methods.

    Quote Originally Posted by SPACE MONKEY View Post
    From what I have seen, with arrays, static methods are expressed within the "[]".
    I'm not sure what you're saying here, but I don't think you're right. The brackets are always array index accessors- nothing to do with static methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Missing a line or two of code,b ut dont know what it is.
    By backdown in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 20th, 2011, 09:13 AM
  2. [SOLVED] New line and StringBuilder
    By newbie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2011, 09:42 PM
  3. Structured Properties File?
    By moka in forum Java Theory & Questions
    Replies: 7
    Last Post: October 19th, 2010, 12:14 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM