• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

What IS an enumerated data type, after all, in Java?

I looked for an example online for using Java enumerated data types, and I found a good one.

I started playing with it using code editor help features in JDeveloper, and I can get to some things going with it now. I can see some things it does, but I just don't know how, and what else can it do for me?

This, and what else I found about enumeration makes me suspect that an enum data type is almost like a "special inner class instance" since you have methods defined inside it. And you can access "fields" in this class.

I don't know though, than other than the 2 "special methods" defined inside the example if there
is anything else or what else that I can define?

            Status[] statusValues = Status.values();      // What is this?  An array of WHAT?
                                                                 // I know an array of Status data types. But
                                                                 // what IS a status data type?

           //  Status instanceOfWhatEver = Status.valueOf(CLASS<T>,String);  

           // What is this method all about?  What is CLASS<T>?

Can someone shed some more light about the theory of enumerations in Java or where I can get
more good explanation or some more practical examples on efficient use?

See my attached example code for reference of what I have done and what I don't quite understand.

Thank you.

package enumexamples;


public class EnumExample {
    
    // use Enum data type to group constants together:
    // Here we have two attributes of a status – an int value and a String description.
    
    private enum Status{                                    // Note:  You can make it public.
                    STATUS_OPEN(0, "open"),
                    STATUS_STARTED(1, "started"),
                    STATUS_INPROGRESS(2, "inprogress"),
                    STATUS_ONHOLD(3, "onhold"),
                    STATUS_COMPLETED(4, "completed"),
                    STATUS_CLOSED(5, "closed");

                    private final int status;
                    private final String description;

                    Status(int aStatus, String desc){
                            this.status = aStatus;
                            this.description = desc;
                    }
                    public int status(){
                            return this.status;
                    }
                    public String description(){
                            return this.description;
                    }
            }       
    
   

    public static void main(String[] args){
                    
                  System.out.println("Get enumeration of all enum values:");  
                  for (Status stat : Status.values()){
                           System.out.println(stat + " value is "+ new Integer(stat.status()) + " desc is " + stat.description());
                   }  
        
        
                    System.out.println("------------------------------------");
                    System.out.println("Status.STATUS_INPROGRESS = " + Status.STATUS_INPROGRESS);                           // Status.STATUS_INPROGRESS = STATUS_INPROGRESS
                    System.out.println("Status.STATUS_STARTED.description() = " + Status.STATUS_STARTED.description());     // Status.STATUS_STARTED.description() = started
                    System.out.println("Status.STATUS_CLOSED.status() = " + Status.STATUS_CLOSED.status());                 // Status.STATUS_CLOSED.status() = 5
    
                    Status[] statusValues = Status.values();      // What is this?  An array of WHAT?
                                                                 // I know an array of Status data types. But
                                                                 // what IS a status data type?

                    //  Status instanceOfWhatEver = Status.valueOf(CLASS<T>,String);                                        // What is this method all about?  What is CLASS<T>?
                    
                    System.out.println("Comparison of what? = " + Status.STATUS_INPROGRESS.compareTo(Status.STATUS_CLOSED));  // Comparison of what? = -3  I guess it must be the position of the enum as it is defined.
                                  
    }
}

Open in new window

This is how the above code runs:

Get enumeration of all enum values:
STATUS_OPEN value is 0 desc is open
STATUS_STARTED value is 1 desc is started
STATUS_INPROGRESS value is 2 desc is inprogress
STATUS_ONHOLD value is 3 desc is onhold
STATUS_COMPLETED value is 4 desc is completed
STATUS_CLOSED value is 5 desc is closed
------------------------------------
Status.STATUS_INPROGRESS = STATUS_INPROGRESS
Status.STATUS_STARTED.description() = started
Status.STATUS_CLOSED.status() = 5
Comparison of what? = -3
Process exited with exit code 0.

Open in new window

0
AttilaB
Asked:
AttilaB
  • 4
  • 2
  • 2
  • +1
1 Solution
 
for_yanCommented:

One thing is to look here (such method as ordinal() you dodn;t mention:
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Enum.html
0
 
for_yanCommented:
0
 
for_yanCommented:
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CEHJCommented:
>>
// I know an array of Status data types. But
// what IS a status data type?
>>

It's an array containing all the instances of the enum (Status) that you defined

>>// What is this method all about?  What is CLASS<T>?

It's the class member of the enum you define:Status.class

Yes, they are essentially static classes. All is explained here

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
0
 
for_yanCommented:
I think there is also concise and good explanation and references in Wikipaedia article about Enumerations in the java section
http://en.wikipedia.org/wiki/Enumerated_type#Java
and look at the references in there
0
 
AttilaBAuthor Commented:
Thank you for referencing and explaining my code and pointing me to a very good tutorial page.

So, I guess CLASS<T> can be any class that I define somewhere that
I am inserting instances of into the enumeration. Right?
0
 
stachenovCommented:
// What is this?  An array of WHAT?
// I know an array of Status data types. But
// what IS a status data type?

Not exactly "data types". It's an array of Status instances. Enums in Java are almost exactly like regular classes except that there is only a fixed set of possible values. You could create thousands of instances of Objects, but you can only have so many instances of a single enum as you have defined (6 in your case). When you list enum constants, you actually create static fields initialized with the constructor arguments you specify.

Now every enum instance has a "hidden" field accessible with the ordinal() method. It is a number ranging from 0 to number of enums minus one. In your case it happens to be equal to the "status" field, but if you reorder your constants, the ordinal will change according to the new order. It is the same order that the elements in the values() array have, and it is used for comparing also.

This is all very much like the singleton pattern with the exception that there is not only one instance, but rather a fixed number of instances. Enums with a single element often used as singletons as they provide quite strong thread safety guarantees.

Also, an enum isn't a "special inner class instance" for two reasons. First, it can be defined outside any class, just as any other regular class. Second, even if defined inside another class (possibly even another enum!) enums are implicitly static so they aren't inner, but just nested.
0
 
CEHJCommented:
:)

>>
So, I guess CLASS<T> can be any class that I define somewhere that
I am inserting instances of into the enumeration. Right?
>>

Yes, so for enum 'E' it would be E.class
0
 
AttilaBAuthor Commented:
Thank you for all your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now