• 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

Understanding enum bodies

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I’m trying to figure out the rhyme or reason for the various valid configurations for enum bodies.



BUT if there’s a method or variable then you need either a ; or a {;

OR

OR

OR

BUT NOT


//you can do this:

//and you can do this:

//but you can't do this:



Am I looking for an explanation when there isn’t one?

Many thanks.

Matt

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. When there is no statement following the last enumeration item, ";" is optional. Well , String[] s = {"1","2"} where ";" is not necessary. I think the syntax of enum is similar to this array's syntax.

enum Weather{RAINY, Sunny} //compiles
enum Weather2{RAINY, Sunny;} //compiles
enum Weather3{RAINY, Sunny}; //compiles



2. When there is a statement following the last enumeration item, a ";" is needed, just like each statement must be followed by ";"

enum WeatherX{RAINY, Sunny;
int x;}



3. enum is a special type of class that extends Enum class. Sunny is an enum, which is an anonymous inner enum and declare variables. It is similar to defining anonymous inner class.

Inside the anonymous inner enum, you can define methods inside it.
enum Weather{RAINY, Sunny
{
void fx1(){}
}
}

4. Inside an enum, you can define a method, so that RAINY.fx1() can be called.
enum Weather{RAINY, Sunny;
void fx1(){}
}

5. There are two issues here. First, BIG(8) means we need a constructor of ICoffeeSize4. Similar to class , if you need an overloaded constructor if you want to create an object with some variable initialized.
Second, OVERWHELMING seems like an anonymous inner enum. Similar to anonymous inner class we cannot define any constructor for an anonymous inner class.


Correct me if I am wrong.
 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Helen.

It is all helpful.

The only thing I noticed about your code is I don't think String [] s = {"1", "2"} will compile without a semi-colon.

Thanks again.

Matt

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Matt.
String[] s = {"1","2"} ;
The semicolon is needed to indicate the end of the statement. But there is no semi colon after "2" like this String [] s = {"1", "2" ;}
But in enum, there can be a semi colon after the last enum like enum Days {M, T, W;}

Correct me if I am wrong.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic