aspose file tools*
The moose likes Beginning Java and the fly likes Using Enumerations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Enumerations " Watch "Using Enumerations " New topic
Author

Using Enumerations

Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45



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 ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

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?.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

What we do here is something like overiding the method defined in the Enum, inside the Enum constants. Correct me if I'm wrong.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

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

Joined: Mar 24, 2008
Posts: 3832

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Enumerations