• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Enumerations

 
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


hi in the above code i have defined a enum Test. in b and c i have defined a method as argument. now in the last of enum i have defined a test() method without a body. now if i remove this test() from enum Test, the compiler gives error at 1 and 2 in main class. so i am not getting why is it so???
[ August 18, 2008: Message edited by: PUNEET MITTAL ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It only accepts methods that are part of the interface of the entire enumeration. You can compare it to anonymous classes - you can define your own public methods, but nobody will know they are there.

Consider the following:
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob, but what i mean to say is that, why do we require that test() method in the last, like is it necessary to give that test() method?.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What we do here is something like overiding the method defined in the Enum, inside the Enum constants. Correct me if I'm wrong.
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes we are overriding the methods here, but what is the need to override these methods. why cant we read those methods directly in other class by just creating a object to it. like if i dont give test() in the last of enums then can i use the test() methods defined in enum constants b and c. if so then how and if we cannot use them then why???
please tell me this concept, i am totally confused in this.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. It's like a,b.. are act like instances of the Enum (actually when we say "Enum.Constant" it creates a new object of that type), what we do is overiding those methods defined in the Enum in it's constants. So those methods should be defined in the Enum.
 
reply
    Bookmark Topic Watch Topic
  • New Topic