Why use enumerations in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

In this article we are going to understand why it is advisable to enumerations in java.

Consider a situation :-

  • We want a class Studentsuch that it has a name and the department it belongs to. We want to create student objects.
  • The department can always be one of Information Technology, Electrical Engineering, Civil Engineering, Electronics Engineering.

How would we achieve this? Suppose we wrote Studentclass as follows.

 

Consider a programmer who is creating a student with one department and another programmer is creating another student with same department. But they don’t know exact names of the departments So, he creates the student as Student prasad = new Student("Prasad Kharkar", "Information Technology")and another one creates Student sushant = new Student("Sushant Pangarkar","Info. Technology")

Both programmers think they have added students to the department Information Technology but for the program, Information Technology and Info. Tech are two different things and these two student will be allocated to different departments. We don’t want that. Thats why java provides a way to make programmers input only the predetermined values.

These are called Enumerations. This is straight from javadoc

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

 

  • An enum specifies a list of constant values of a specific type
  • An enum type is not some String or int, it is the Enum type.
  • An enum can have constructors and methods. You can override toString() method to provide meaningful intrepretation of the enum type.
  • An enum specifies a list of constant values of a specific type
  • An enum constructor cannot be invoked directly, we have to use only the predefined values.

So we will create a Department type and modify Student class to take the department in the Enum format.

 

Now when programmers are trying to create the Student object, they know for sure that Department types should be passed to Student constructor. So, student objects will be created using,

This solves the probelm of confusion while creating student object. But how do we specify some meaningful information for each enum type? We use constructors and override toString() method for Department as follows.

Let us put our Student
class to the final test.

This will print Electrical Engineering and Power and provides meaningful representation of the department.

I hope this article helps understand the advantages of using enumerations.

Share Button

3 comments for “Why use enumerations in java

  1. March 8, 2015 at 10:28 am

    Wonderfully explained and nicely documented. I came across this link, while looking at one of yours answers at StackOverflow ( http://stackoverflow.com/a/17607004/1057230 ). Really do appreciate your work. KEEP SMILING 🙂

    • March 9, 2015 at 4:18 pm

      Hi Gangadeep, I’m glad it helped. Happy learning

  2. August 22, 2016 at 2:11 pm

    Nice artical Prasad….good example of enum class

Leave a Reply

Your email address will not be published. Required fields are marked *