• 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 optional semicolon

 
Ranch Hand
Posts: 36
jQuery Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?:

 
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you show me an example where it is required to have a semi colon?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic