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("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_STARTED.description() = started
Status.STATUS_CLOSED.status() = 5
Comparison of what? = -3
Process exited with exit code 0.

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


One thing is to look here (such method as ordinal() you dodn;t mention:
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I think there is also concise and good explanation and references in Wikipaedia article about Enumerations in the java section
and look at the references in there
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?
// 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.

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
AttilaBAuthor Commented:
Thank you for all your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.