| Author |
Need Programming Help With the "enum" Type of Variable
|
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
I have never used variables with the "enum" type. I have several questions in regard to its uses. The questions are marked in the code snippet show below. I appreciate your help. Thank you.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
|
Hi Natalie, your code could look as follows:
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
I have modified my code to the one shown below:
Question: Does the comparison of RequestTypes.PATH_ONE.equals("PATH_ONE") is evaluated to true? where requestType = RequestTypes.PATH_ONE;
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
If you look in the Java Language Specification, you find that enums are true singletons, ie the JVM only ever has one instance of each. That means you can use the == operator.
Try this sort of thing:-There is also a section in the Java Tutorials.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
. . . which tells you how to use enum constants in a switch-case structure. Maybe that will help you.
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
|
Thanks very much for your help and support. Now, I have a better idea about "enum".
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
I am still a little confused. If you would not mind helping again:
The question is if we have an "enum" variable A. And another variable B is assigned a value of one of the elements in variable A. What is the data type of variable B? Thanks.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3859
|
|
The type is the name of the enum: RequestTypes
enums are a specialised type of class, so you'd use the name of the enum in the same way as you'd use the name of the class. Behind the scenes, the compiler will create a class called RequestTypes.
One way of confirming this would be to add this somewhere:
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
|
Thanks very much.
|
 |
 |
|
|
subject: Need Programming Help With the "enum" Type of Variable
|
|
|