|
|
Enumeration Enum = ..... - Variable identifier a keyword? | ||
Discussion by eubanksd with 4 Replies.
Last Update: June 5, 2008, 2:01 pm | |||
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.
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.
Wed Mar 7, 2007 Reply New Discussion
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:
At the same time there exists a class called String, which you can use in a similar fashion.
Syntax:
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.
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:
CODE
string myString;At the same time there exists a class called String, which you can use in a similar fashion.
Syntax:
CODE
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
Thu Mar 8, 2007 Reply New Discussion
It's because enums were not supported in Java until 1.5. 1.4 can still use enum as a variable, since it did not become a reserved token until 1.5.
Fri Mar 9, 2007 Reply New Discussion
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(c); }
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
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(c); }
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
Mon Apr 28, 2008 Reply New Discussion
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
Enumeration Enum = .....
Int param1;
String mystring = "param1";
Is there any way I can compare the variable name to the string??
-question by dev
Thu Jun 5, 2008 Reply New Discussion
Scrutinize My Chess Game! Check out my coding skillz! (6)
|
(3) Arraylist Issues
|
Index




