Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about optional semicolon in enum declaration (K&B7)

 
Brian Linton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Safari books edition:

In chapter 1, in the enums section you guys wrote in the declaring enums section:
To make it more confusing for you, the Java language designers made it optional to put a semicolon at the end of the enum declaration (when no other declarations for this enum follow)


Well, I thought about that a bit and the following code will compile (filename SemiColon.java):


So I'm wondering if that statement you wrote is valid or what you meant?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian Linton,

First of all, a warm welcome to CodeRanch!

Brian Linton wrote:So I'm wondering if that statement you wrote is valid or what you meant?

It just means that in an enum declaration the semicolon is optional if no other declarations for this enum follow.

In your code snippet you didn't have any enum declaration at all. So I'll illustrate this with a few code examples. This code snippet declares an enumNow because this enum declaration doesn't have any other declarations the semicolon is not needed and can be omitted. So this code still compiles successfullyNow if you add another declaration to this enum, for example an additional method the semicolon add the end is required. So this code compiles successfullyBut this code doesn't; you'll get a compiler error because the semicolon is missing

Hope it helps!
Kind regards,
Roel
 
Brian Linton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! It makes sense now what the book is referring to.
 
Brian Linton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thanks for the welcome! Should have been active on this site for 10 years now.... then again... should have gotten SCJP years ago.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Linton wrote:Should have been active on this site for 10 years now.... then again... should have gotten SCJP years ago.

Better late than never!
 
Brian Linton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Now if you add another declaration to this enum, for example an additional method the semicolon add the end is required. So this code compiles successfullyBut this code doesn't; you'll get a compiler error because the semicolon is missing


Ah! Later on in the chapter there's a bit more confusion.... check out the two minute drill:
The semicolon at the end of an enum declaration is optional. These are legal:


Note that the semicolon is OUTSIDE of the curly brace. Which is kind of what I was pointing out. I was trying to illustrate that you can have extra semicolons in a lot of places as I think this code snippet is doing. It REALLY should read:


I think the book was illustrating the semicolon OUTSIDE the curly brace.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Linton wrote:I think the book was illustrating the semicolon OUTSIDE the curly brace.

I guess you are correct! I was a little bit mislead by the "(when no other declarations for this enum follow)" part.

But the study guide is still correct: the semicolon after the enum declaration (so the one outside the closing curly brace) is optional. So both code snippets will compile successfully

The required semicolon when more code follows the enum constants (inside the enum declaration) is covered at page 65. So I guess from the original sentence
To make it more confusing for you, the Java language designers made it optional to put a semicolon at the end of the enum declaration (when no other declarations for this enum follow)
the part in between parantheses could/should be removed. Because I think that's referring to declarations inside the enum and is therefore not useful in that sentence.

Hope it helps!
Kind regards,
Roel
 
Brian Linton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:The required semicolon when more code follows the enum constants (inside the enum declaration) is covered at page 65. So I guess from the original sentence
To make it more confusing for you, the Java language designers made it optional to put a semicolon at the end of the enum declaration (when no other declarations for this enum follow)
the part in between parantheses could/should be removed. Because I think that's referring to declarations inside the enum and is therefore not useful in that sentence.


Agreed, your example illustrates the requirement pretty well, but I was at a loss when reading the book. Not sure if it's an errata, but maybe a clarification that could be made in a future revision (OCJP 8 book!). I wonder when that's coming out. The test is live now. Going to try it after my OCAJP attempt.

Brian
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Linton wrote:Not sure if it's an errata, but maybe a clarification that could be made in a future revision (OCJP 8 book!). I wonder when that's coming out. The test is live now. Going to try it after my OCAJP attempt.

If you take the OCAJP7 exam, you must take the OCPJP7 exam as well. So if you want to take the OCPJP8 later, you know have to take the OCAJP8 exam. You can not combine different version certifications.

I think you are not the only one wondering about when K&B8 will be released. I guess it will take quite some time, because Java 8 has a lot of new language features like lambdas, predicates, data/time API. But also major changes to existing language features like default and static interface methods. So that means that all text (and questions and explanations) concerning interfaces needs to be reviewed and changed appropriately.
 
Brian Linton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to know Roel! I'm going for 8 on both exams.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic