[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-10
9
Medium Priority
?
445 Views
Last Modified: 2013-11-23
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
Comment
Question by:AttilaB
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36943005

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36943013
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36943018
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36943025
>>
// 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
 
LVL 47

Expert Comment

by:for_yan
ID: 36943489
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
 

Author Closing Comment

by:AttilaB
ID: 36943621
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
 
LVL 4

Expert Comment

by:stachenov
ID: 36943699
// 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36943776
:)

>>
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
 

Author Comment

by:AttilaB
ID: 36944038
Thank you for all your help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question