• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Programming Help With the "enum" Type of Variable

 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Natalie, your code could look as follows:
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:. . . There is also a section in the Java Tutorials.
. . . which tells you how to use enum constants in a switch-case structure. Maybe that will help you.
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your help and support. Now, I have a better idea about "enum".
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic