GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes enum syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enum syntax" Watch "enum syntax" New topic
Author

enum syntax

Red Smith
Ranch Hand

Joined: Aug 05, 2007
Posts: 136
    
    1
It appears that you can write

enum Test { one , two };

or

enum Test { one, two }

and both compile (with trailing semi-colon and without)

Is that correct, the semi-colon is option with an enum declaration?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3599
    
  15
An enum is just an special form of a class definition (that's why they can have constructors, methods and fields) and class definitions don't need a ; at the end.


Joanne
Red Smith
Ranch Hand

Joined: Aug 05, 2007
Posts: 136
    
    1
Originally posted by Joanne Neal:
An enum is just an special form of a class definition (that's why they can have constructors, methods and fields) and class definitions don't need a ; at the end.


Thanks! I didn't realize it was optional for class definitions as well!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

To be honest, despite the two SCJP certificates I have, I didn't think it was legal either. I thought it would be seen as an empty statement, which is not allowed outside methods / constructors / static blocks / initializer blocks, let alone outside classes. Apparently it isn't in this case.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
I'd suspect it happens to be legal because of some inline-class syntax, but you'd have to dig into the minutia to ferret it out.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: enum syntax