wood burning stoves 2.0*
The moose likes Java in General and the fly likes Need Programming Help With the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Programming Help With the "enum" Type of Variable" Watch "Need Programming Help With the "enum" Type of Variable" New topic
Author

Need Programming Help With the "enum" Type of Variable

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
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: 3575
    
  14

Hi Natalie, your code could look as follows:
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
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: 37926
    
  22
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: 37926
    
  22
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

Joined: May 06, 2003
Posts: 325
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: 325
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: 4341
    
    7

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: 325
Thanks very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Programming Help With the "enum" Type of Variable
 
Similar Threads
generic enum and the values() method
Convert from enum ordinal/constant to enum type
Could need enum inheritance / Design question
Issues With Enum
Messy enum problem