This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt in enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in enum" Watch "doubt in enum" New topic

doubt in enum

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
Hi ranchers,
When declaring an enum why is that we have to define the constructor only after defining the enum constants. If we try defining the constructor before the enum constants we get a compile time error. Can someone explain me this. For ex:
//Code that throws compile time error

// Code that works fine.

Thanks and Best Regards,

SCJP 1.6
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

Java requires that Enum constants should be declared first prior any fields , constructor, methods. The constructor automatically creates the constants that are defined at the beginning of the Enum body.

SCJP 5.0, SCWCD 5, preparing for SCDJWS
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37918
That is the way the grammar specifies it. Whoever designed that decided you need to enumerate the possible members before writing the class body.

Don't know any more, but look at the Java Language Specification link I gave you.
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
thanks for the answer.
I agree. Here's the link:
subject: doubt in enum
Similar Threads
How to compile and run this enum class
Usage of Enum...
Doubt in method overriding in enum
newbie's question about constructor