File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understanding enum bodies 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 "Understanding enum bodies" Watch "Understanding enum bodies" New topic
Author

Understanding enum bodies

matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 65
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

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Jan 25, 2010
Posts: 65
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

Joined: Nov 01, 2011
Posts: 451
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding enum bodies
 
Similar Threads
Semicolon after enum constant declaration?
How to call method which was in enum class
Enum programs are not compiling in java5.0 compiler?
Hard Exam question