aspose file tools*
The moose likes Java in General and the fly likes Usage of Enum... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Usage of Enum..." Watch "Usage of Enum..." New topic
Author

Usage of Enum...

Abhijit Kolhatkar
Greenhorn

Joined: Sep 14, 2006
Posts: 10
Hi All,
There is a post by our fellow rancher Ade Barkah, in some of JavaRanch Forum.
I read that and was amazed and confused by his/her findings....
Thanks for that...

I will quote that example:
We can declare an ENUM as a member variable of a class , as follows:

class TestClass
{
enum myEnum
{
RED,
WhatIsThis
{
void AMethodInEnum()
{}
}
}
/** rest of the class defination follows */
...
...
}//end of class
I have a coupl of questions:
1. How can we use this Enum???
2. the method declared inside the enum, doesnt lead to any compile/run time error.. How can we call this method??
3. To what does this method belongs? For sure, it doesnt belong to the class. Does it belong to the enum then?
4. If the method belongs to enum, is it not violating one of the principles of OOPS, implemented in JAVA. I.e. a method can not exists without a class??

Thanks
Abhijit
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi, cowboys!

Abhijit Kolhatkar asked:
1. How can we use this Enum???

I don't refer to the original example since it is somewhat incomplete.

So I made another example:


Cat, Fish and dog are the constants of the enum.
The method is associated with all constants of the enum. It will be the same for all of them unless the method is not overridden for individual constants of the enum.



2. the method declared inside the enum, doesnt lead to any compile/run time error.. How can we call this method??

The method in the original example cannot be called (exception: via reflection).
There should be a method for ALL constants, overriden maybe in certain constants.
It is called like in the animal example.

3. To what does this method belongs? For sure, it doesnt belong to the class. Does it belong to the enum then?

Yes, belongs to the enum.



4. If the method belongs to enum, is it not violating one of the principles of OOPS, implemented in JAVA. I.e. a method can not exists without a class??

No, an enum is a special form of a class. An enum extends class java.lang.Enum<E>.




When you'd like to have your original example return something usefull, you should insert a method for all the constants, as in the following:









Yours,
Bu.


all events occur in real time
Abhijit Kolhatkar
Greenhorn

Joined: Sep 14, 2006
Posts: 10
Thanks a Ton Bu...
All my questions were answered and infact the explanations were pretty helpful too...

Thanks Once again....

Abhijit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Usage of Enum...
 
Similar Threads
static method overiding
enum within a method?
problem with static method..
Related to Enum const specific class body
Enums with numbers