File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes please explain the code and please anyone give example of overloading in enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "please explain the code and please anyone give example of overloading in enum" Watch "please explain the code and please anyone give example of overloading in enum" New topic
Author

please explain the code and please anyone give example of overloading in enum

kapilg gupta
Greenhorn

Joined: Jun 14, 2006
Posts: 10
enum Animals {

DOG("Woof"),CAT("meow"),FISH("burble");
String sound;
Animals(String s)
{
sound=s;
}
}
class TestEnum {
static Animals a;
public static void main(String args[])
{
System.out.println(a.DOG.sound +" "+a.FISH.sound);
}
}
output:-woof burble
i want to ask when values are assigned in sound also how oveloading can be done in enum
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


Above enum code is similar to following code:




Here DOG, CAT, FISH all become named constant of type Animal.

These three named constants DOG, CAT, FISH refer to the three Animals instances and these instances are created when Animals class is initialized after it is loaded.

When you call a.DOG, this will return a instance of Animal.

Each instance has a instance variable sound...


1. a.DOG.sound returns woof

2. a.FISH.sound returns burble

Second part of your question


Originally posted by kapilg gupta:
i want to ask when values are assigned in sound also how oveloading can be done in enum



Since enum is a normal java class, so you can overload constructors/methods in enum just like old java class. Same rule of overloading will also apply here.


regards


Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Following code overloads constructor as well as method.

overloading.





Regards


Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain the code and please anyone give example of overloading in enum