Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum Declaration

 
Geoff Prest
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm a first time poster, long time browser, hoping for clarification on enum declarations. In prep exams I'm taking before I sit for the SCJA, questions regrading enums are coming up that are leaving me a little confused.

To be specific, it is all about the semi-colon.

In one question the correct answer given for an enum declaration is as follows:

enum Grade{a,b,c,d};

However, in another it states that the following code will compile and run, outputting GOLDFISH:

class Test {
enum Fish{GOLDFISH, ANGELFISH, GUPPY}

public static void main(String args[]){
System.out.println(Fish.GOLDFISH);
}
}

In this code the semi-colon following the enum declaration is not present.

I had intitially thought the semi-colon was used when an enum declaration was within a class, and not used when it was a stand alone declaration in a file by itself, but the code above disproves that theory.

So my question boils down to this: Can anyone explain to me when and where the semi-colon is required for an enum declaration to be legal?

Thanks in advance,
Geoff
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I haven't worked much with enums, so maybe I'm missing something. But I think this is just a case of extra semicolons being interpreted as empty lines. They aren't needed, but don't hurt anything...
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is just a case of extra semicolons being interpreted as empty lines


I believe that in the case of enums, the semi-colon is more than just having an empty statement or not. I believe that I read in K&B that for simple declarations of enums such as the Grade example, that the semi-colon is optional, but for more complex examples, the semi-colon is required.

I can't say that I understand this entirely but that's what I read.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I have heard, and it sounds reasonable, is that the spec allows it either way because apparently enums are sometimes declared like an inner class and that, when used in that context, a semi-colon would look unnatural. So, the spec allows it either way so that you can define one inline with other code or in an "inner-class like" manner.

I hope it makes sense to somebody....
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been using a "complicated" enum, defined in a class by itself - when I delete the semicolon after the final member definition, it will no longer compile -

This may cast more fog than light, but now I'm confused too, because I tried the Fish example as well, and it compiles without the semicolon.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jinny Morris:
I've been using a "complicated" enum, defined in a class by itself - when I delete the semicolon after the final member definition, it will no longer compile...

If you're coding additional functionality for your enum, then you do need a semicolon after the final element (before your additional functionality). See the "Planet" example in this enums page. However, note that there is no semicolon following the closing brace of the enum definition itself.
 
Geoff Prest
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all very much for your interest and responses. All of them put together have given me the clarity I was searching for.

Using 1.6.0_01 I tested every declaration configuration option I could think of. The testing showed that the use of a semi-colon at the end of a declaration is indeed 100% optional whether the enum is declared within the class, outside of the class (before or after) but in the same file, or in a file of its own. Both of the following are valid and legal.

enum [name] { [constant1], [constant2], [etc] }

and

enum [name] { [constant1], [constant2], [etc] };

The non-optional semi-colon in an enum declaration immediately follows the list of constants if variables, constructors, and/or other methods are included.


Thanks again everyone,
Geoff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic