• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enum Declaration

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic