Jump to content
xisto Community
Sign in to follow this  
eubanksd

Enumeration Enum = ..... Variable identifier a keyword?

Recommended Posts

Oseveral occasions when I was perusing example source code from various locations, I see something like this:Enumeration enum = headers.keys();while (enum.hasMoreElements()) {String name = (String) enum.nextElement();Forgive me here, but to me it looks like they're declaring a variable's identifier to be a java keyword...? I've seen this more than once, but ran across it just now in the com.oreilly.servlet package source. Eclipse always gives me errors when I load example code containing something like this, so I'm sure it's incorrect. What's the deal? Was enum formerly not a keyword, and this is old code?And I have another question: I tried my darndest but couldn't figure out how to get Eclipse to recognize com.oreilly.servlet package when I tried to import it; it always says it's unknown or something, so I know I have to make the IDE or compiler or whatever aware of this package, but how do I do that? Put the package in a special folder where these sorts of things go, and where my IDE will find it automatically? Or add it to some list or other from within the IDE...Thanks for your help.

Share this post


Link to post
Share on other sites

True. This has me confused too. As far as I know the enum keyword hasn't been deprecated from any of the supporting languages and nor are there any signs of being so. An enumeration list if used wisely, is by far one of the most useful tricks in a coders bag.

 

However, a lot of the recent OOP based languages have introduced similar named classes that somewhat behave the same way as simple variable identifiers, with added enhancements.

 

For example, in .NET string is a variable type declarator.

Syntax:

string myString;

At the same time there exists a class called String, which you can use in a similar fashion.

Syntax:

String myString;myString = new String();

In the second case a lot of member functions are available under the String class with which you can do some cool stuff with the string stored in the class.

 

I believe in your example, Enumeration is also a class that helps you build enumeration lists just like enum but provides additional iterative methods like hasMoreElements() & nextElement() which wouldn't be available to an enum list under normal circumstances.

 

What baffles me is the use of enum as a variable name ;). I don't see how that is possible. enum I believe is still a reserved keyword. I'll see if I can find an explanation for this. In the meantime if you come across any, do write back. It'll be interesting to know how this works out.

Share this post


Link to post
Share on other sites

Could u plz find the ans for the below qn with the proper explanation..???

Enumeration Enum = .....

 

11. Public class Ball {

12. Public enum Color { RED, GREEN, BLUE };

13. Public void foo() {

14. // insert code here

15. { System.Out.Println©; }

16. }

17. }

Which code inserted at line 14 causes the foo method to print RED,

GREEN, and BLUE?

A. For( Color c : Color.Values())

B. For( Color c = RED; c <= BLUE; c++)

C. For( Color c; c.HasNext() ; c.Next())

D. For( Color c = Color[0]; c <= Color[2]; c++)

 

-reply by Krish

Share this post


Link to post
Share on other sites

compare variable names to strings

Enumeration Enum = .....

 

Int param1;

String mystring = "param1";

 

Is there any way I can compare the variable name to the string??

 

-question by dev

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.