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.
enum: can anyone post the rules for enum declarations
madhu v pe
Ranch Hand
Joined: Apr 21, 2007
Posts: 100
posted
0
Hi,
can anyone post the rules for enum declarations and usage? or provide the link where can I get the restrictions. I saw in java.sun.com but could not get all the rules.
Thanks in advance
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
Hi Madhu,
I see the following rules for the enum:
1- All the constants must be declared on the top, they can't come last or in between enum definition. 2- You must see the constructor whether it is defined or not for the arguments specific to all constant constructor call. If Color("Green"), Color(13) constants are there, there must be constructor with String and int argument. 3- Constructor is by default private. You can at most make is default nothing else. It can't be public. 4- If you see any abstract method declaration in the enum, you must see whether there is definition given to that method with each enum constant. 5- You must see the semicolon in case there are constructors/methods defined besides only constants definition. 6- ordinal() method tells the position of particular enum constant in the enum. 7- enum can't be defined inside the method. 8- enum constructor can't be directly called. 9- If you use one enum constants, constructor will be called for all the enum constants.
Thanks,
cmbhatt
madhu v pe
Ranch Hand
Joined: Apr 21, 2007
Posts: 100
posted
0
Thanks Chandra, Pls post the source from where you have gathered all these points.
Originally posted by Chandra Bhatt:
1- All the constants must be declared on the top, they can't come last or in between enum definition. 2- You must see the constructor whether it is defined or not for the arguments specific to all constant constructor call. If Color("Green"), Color(13) constants are there, there must be constructor with String and int argument. 3- Constructor is by default private. You can at most make is default nothing else. It can't be public. 4- If you see any abstract method declaration in the enum, you must see whether there is definition given to that method with each enum constant. 5- You must see the semicolon in case there are constructors/methods defined besides only constants definition. 6- ordinal() method tells the position of particular enum constant in the enum. 7- enum can't be defined inside the method. 8- enum constructor can't be directly called. 9- If you use one enum constants, constructor will be called for all the enum constants. Thanks,
I have read, java.sun.com apart from some of points you have mentioned, one more point is 10. non-inner enum cannot be declared static.
and I have doubt on below source
when it calls toString() method? while using SOP??