aspose file tools*
The moose likes Beginning Java and the fly likes Enum Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum Declaration" Watch "Enum Declaration" New topic
Author

Enum Declaration

Geoff Prest
Greenhorn

Joined: Feb 20, 2007
Posts: 17
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


SCJP 6, SCJA, SCJD in progress
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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

Joined: Jun 04, 2007
Posts: 320
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....


------------------------
Bob
SCJP - 86% - June 11, 2009
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 20, 2007
Posts: 17
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
 
Consider Paul's rocket mass heater.
 
subject: Enum Declaration