wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 3429
    
  12
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: 19654
    
  18

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);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: enum syntax
 
Similar Threads
Enums
How use Enum in Java 6.0
A Simple Control Flow Question
syntax
Enum Declaration