• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum optional semicolon

 
Jelo Nehuptra
Ranch Hand
Posts: 36
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you show me an example where it is required to have a semi colon?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 36
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic