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
Code Java:
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
Code Java:
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
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:
Code java:
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:
Code java:
BreakfastItem breaky = BreakfastItem.values()[ r.nextInt(8)];
Is pretty much doing this:
Code java:
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)
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. :-h
Re: enum, value of: Why is this code line structured the way it is?
Quote:
Originally Posted by
SPACE MONKEY
Going to chew on this one for a wile. Thanks for your help.
My brain in hurting though. :-h
No problem. Is there anything about my explanation that wasn't clear?
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 "[]".
Re: enum, value of: Why is this code line structured the way it is?
Quote:
Originally Posted by
SPACE MONKEY
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
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
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.