• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in enum

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Pradeep
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic