This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum - What's wrong with this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "enum - What Watch "enum - What New topic
Author

enum - What's wrong with this code?

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 334


Thanks for any help!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Faisal,

What I see in your code is that the constants are not placed what they must be.
That is why it is compiler error.

Place constants beginning of the enum definition.

All is well then.


Thanks,
[ May 04, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 334

Thanks for your reply!
Well, is that a rule? I was actually writing the code as I usually write for any other class..first instance varibales, then constructors etc..
enum is also a class, right? Then, why such different behavior? Know any explicit reasons?
Thanks again!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
It is mere a rule and I don't find any reasoning behind that yet:

What java doc says:


Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.



Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.


Thanks,
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 334

Wow!!
That's great to know.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: enum - What's wrong with this code?
 
Similar Threads
enum clarification
What is Enum?
enum constructor private