Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to learn to answer my own questions  RSS feed

 
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm busy covering enumerators, where the materials covers the following example:

"You can specify values of enum constants at creation time, as follows:"



I understand that part. Then the exercise asks us to create enums with multiple values, without explaining how.

Ok great, self learning time. So off I go to Google to find out if that is possible, and I come across the following example:



The code that follows that example then loses me further as it talks about varags and other ideas that I have not come across. So I go read up about varargs, but I can't see how the example I have covered relates in any way to the enum problem.

Where I am struggling at the moment is, how do you even know what is possible?

So off I go to the Java docs:

https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html

I read the page and my eyes just glaze over. The page is written at a level that is too deep for me to comprehend right now. I have no idea how to turn what I read on that page into a way to understand that I can pass multiple values to an enum.

What do I need to do I overcome this hurdle?
 
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My "ah ha!" moment in learning enums is to realize that there is a lot of over lap between classes an enums. So enums can have multiple member variables and when you declare an enum (e.g. MICHIGAN) it can have multiple parameters. You then need a constructor that will take those parameters.
 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I do in these situations is google something like java enum example, which gives me a nice page:

http://www.ntu.edu.sg/home/ehchua/programming/java/javaenum.html

Skimming it, it looks good.  Down in section 2 (I believe) they talk about enums with parameters.
 
Jay Rex
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:My "ah ha!" moment in learning enums...


While my example dealt with enums, it could have been any part of Java. I'm covering Iterators next, and I am sure I will run into the same problem.

Knute Snortum wrote:What I do in these situations is google something like java enum example, which gives me a nice page:


That is what I was trying to do with my own searching yesterday. As you will see in the example you sent me, it only covers single value constants.

I appreciate that my course material asked me to do something that I didn't know was possible as it has forced me to broaden my knowledge base. If they kept the example simple, I wouldn't know that this is possible.

So let me ask the question like this: Have a look at the enum Java doc page. What part of that page, if read correctly, would have helped me realise that enums can take multiple values of any type?

 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:So let me ask the question like this: Have a look at the enum Java doc page. What part of that page, if read correctly, would have helped me realise that enums can take multiple values of any type?


Unfortunately, the answer is this part: "section 8.9 of The Java™ Language Specification."

You would then Google for that text and get to here: https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9

Then you'd have to start slowly scanning that section. When you get to 8.9.1, you will see:

EnumConstant:
   Annotations(opt) Identifier Arguments(opt) ClassBody(opt)

Arguments:
   ( ArgumentList(opt) )

[Note: the "(opt)" above are actually shown as subscripts in the JLS, not surrounded by parentheses. It means that particular item is optional.]

I have bolded the parts that say you can have multiple arguments. Beginners like you are likely to miss this though because you probably won't know what you're looking at and will just glaze over this.

I am lucky to have been taught in school (almost over 30 years ago now) how to read Bachus-Naur Form (BNF) notation and syntax diagrams. I doubt they teach kids this skill anymore. Good thing there's JavaRanch where there are old-timers like me who still know this stuff and can tell young whippersnappers like you to learn it, right?

[EDIT: I learned to read BNF and syntax diagrams mid-way through college, so yeah, it's been over 30 years now]
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wrote:...
You would then Google for that text and get to here: https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9
...
EnumConstant:
   Annotations(opt) Identifier Arguments(opt) ClassBody(opt)

Arguments:
   ( ArgumentList(opt) )

[Note: the "(opt)" above are actually shown as subscripts in the JLS, not surrounded by parentheses. It means that particular item is optional.]


Even that path is fraught with misfortune. I don't know if you get different results but when I do a search, Google seems to like to offer the JLS link for JDK 7 first rather than the newer versions. The BNF in the JLS for JDK 7 leaves much to be desired. In the JDK 7 version, there's no way to easily find out what the definition of "ArgumentList(opt)" is.

They got better with JDK 8 onwards, so whenever you find a link to the JLS for JDK 7, change it to the one for JDK 8 or later. You can simply change the numbers in the URL like so:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9 (same section for JDK 8)
https://docs.oracle.com/javase/specs/jls/se10/html/jls-8.html#jls-8.9 (same section for JDK 10)

The syntax notation in these versions provide links that you can then follow to dig into the details of each element of the syntax. In this case, you'd click on the "( ArgumentList )" link and get to this definition:

ArgumentList:
Expression {, Expression}

Where "Expression" is a link to yet another definition. And yes, these links can run pretty deep so if you're a digger, then you could have a lot of fun in front of you. ;)  Happy digging!
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you're curious about how I learned to program, my first programming language was Pascal, which was the main instructional language being used at the time, besides C and BASIC. This was in the mid- to late-80s, in the Philippines. Our textbook was "Programming in Pascal" by Peter Grogono. This book included BNF and syntax diagrams for all the language elements that were introduced so that's why I'm very comfortable with reading those sections of the JLS.

In fact, earlier this year when I started to get a sense that schools no longer formally included these topics as part of their introductory programming courses, I bought that book to remind myself how lucky I was to be kind of an old-school programmer and that being trained in the "old-school" ways wasn't necessarily a bad thing. I still browse through my copy of that book when I wax nostalgic.
 
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Unfortunately . . . "section 8.9 of The Java™ Language Specification." . . .

What is unfortunate about that section of the JLS? The more recent versions have severall tutorial‑like examples, including cards and arithmetic, so we can direct newbies to there and, believe it or not, they get the solutions to their homework there.. And if Oracle are going to publish it in the JLS, who is able to complain about their using those examples?
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . Unfortunately . . . "section 8.9 of The Java™ Language Specification." . . .

What is unfortunate about that section of the JLS?


It's not about that section in particular but the JLS in general. I seem to recall that it's you who often warns people that the JLS is difficult to understand.
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . it's you who often warns people that the JLS is difficult to understand.

Somebody, I forget who, told me not to scare people off reading the JLS at all. But that only applies to 99% of the JLS. The arithmetic enum and the card enum are the other 1% 
 
Knute Snortum
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:

Knute Snortum wrote:What I do in these situations is google something like java enum example, which gives me a nice page:


That is what I was trying to do with my own searching yesterday. As you will see in the example you sent me, it only covers single value constants.


Did you see section 2.1 and 2.2?  Is section 2.1 what you mean by "single value constants"?  What you can do with one value, you can do with several values if you want.  Sometimes examples you find don't cover exactly what you want, but they provide clues to get you there.

http://www.ntu.edu.sg/home/ehchua/programming/java/javaenum.html
 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually search for 'Java <subject> tutorial  oracle' or similar, to get to the orace tutorials. In this case they have a  very clear article about the use of enums:

tutorial
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:Then the exercise asks us to create enums with multiple values, without explaining how.

Ok great, self learning time. So off I go to Google to find out if that is possible, and I come across the following example:

The code that follows that example then loses me further as it talks about varags and other ideas that I have not come across. So I go read up about varargs, but I can't see how the example I have covered relates in any way to the enum problem.

Where I am struggling at the moment is, how do you even know what is possible?


As you can see from the replies, there are many paths your quest for learning and enlightenment could take you. There's an element of luck to it but I think, as with gambling, given enough tries, a pattern starts to emerge and things start to average out. The house always wins and luckily, in this case, you are "the house." That means you just have to roll the dice a lot of times. That means you have to read a lot of examples. If you look at the different replies, you'd realize that if you had tried them all, at least one of them would have gotten you to the information you needed.

Certainly, coming to a site like this and asking questions will help speed up the process. As a beginner, you don't know what you don't know, right? Take that example you cited. I immediately got suspicious with the varargs thing and the way that enum constructor took in a boolean parameter. To me, a boolean parameter is a code smell that needs to be checked out. After a few attempts to find that code, I found a thread on SO that had this initial code:

(NOTE: This is NOT the best example for how to use enums in Java)

On that site, it's probably best to NOT emulate the first code example listed. If you read through the responses in that thread, they actually give the OP another version that addresses some of the problems with the original code. Even with the refactored example given later in the code, I think there are still some more improvements that can be made. I'll create a thread in our Code Review forum to discuss that in detail.

Anyway, you can increase your chances of finding the right information you need to learn by reading as many examples as you can and trying to understand what the examples are showing. One way to avoid getting down into a rabbit hole like you almost did with digging into varargs is to find as many different examples you can find and pick the simplest one to study first. Once you understand that one, go on to the next simplest one and experiment with it to get a better understanding. Always be skeptical of code you find and look for any flaws in the design. This requires a lot of practice.
 
Jay Rex
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Unfortunately, the answer is this part: "section 8.9 of The Java™ Language Specification."


Thank you for your detailed comments.

Piet Souris wrote:I usually search for 'Java <subject> tutorial  oracle' or similar, to get to the orace tutorials. In this case they have a  very clear article about the use of enums:

 
Good advice, thank you.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!