aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum optional semicolon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum optional semicolon" Watch "Enum optional semicolon" New topic
Author

Enum optional semicolon

Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

Why is it that on page 62, it says that

"To make it more confusing for you, the Java language designers made it optional to put a semicolon at the end of the enum declaration (when no other declarations for this enum follow)"

but why does this compile?:

Viktor Pergjoka
Greenhorn

Joined: Aug 30, 2010
Posts: 20
Hi,
the enum declaration defines a class (enum type).

It is similar like this:


public class EnumExp {

enum H{H} // enum type class
enum UH{} //enum type class
int x = 0;
String s = "h";
static class foo{ //static foo class
}

class fooFo{ // inner fooFo class
}

.....

}
Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

can you show me an example where it is required to have a semi colon?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

Enums are just (special) classes, so you can add methods and other declarations to them. If you do that, you need the semi-colon first.

Here's an example.
Here, I've got an enum with a private member, an accessor (getter) for it, and a constructor to set the value. You need the semi-colon so the compiler can tell where the enum values end and the rest of the class description begins.
Viktor Pergjoka
Greenhorn

Joined: Aug 30, 2010
Posts: 20



You can see that after the declaration of the members (RED, GREEN, BLUE) follows a method declaration (here the toString() method).

Here is another example:



Again here follows after the declaration of the members another declaration (here the constructor).
Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

I thought it was the enum declaration as a member of a class that the semi colon was optional as in:



not the:



because the book states:

To make it more confusing for you, the Java language designers made it optional to put a semicolon at the end of the enum declaration (when no other declaration for this enum follow):



So what is an enum declaration?

is it the declaration of an enum in the class or the declaration of the values of an enum? what are the proper terms if you may please?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum optional semicolon