GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum: can anyone post the rules for enum declarations 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 "enum: can anyone post the rules for enum declarations" Watch "enum: can anyone post the rules for enum declarations" New topic
Author

enum: can anyone post the rules for enum declarations

madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
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
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
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??

Pls help

Thanks
 
GeeCON Prague 2014
 
subject: enum: can anyone post the rules for enum declarations