• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

enum: can anyone post the rules for enum declarations

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic