As we know, java is a object oriented language and composed of classes. So it is similar to C++ as object oriented but java is loaded with advanced features which can make it more powerful.
“With great power comes great responsibility”.
So we will start out discussion with, what are classes in java? Class is a set of properties or methods that are common to objects of that particular class. In java the class is declared by class keyword as same in C++. While declaring the class first we have to use class keyword, then class name we want to give. Also mention the modifiers for the class i.e. public, final, abstract, etc. Then we can add the methods and data members of the class, this is nothing but the body of the class.
So Before going to enums let us discuss about access modifiers in java. So modifiers (Access Modifiers) are basically keywords in java which are used to specify the accessibility or scope of a field, method, constructor or a class. Here let us talk about 3 access modifiers in java which we will be needing to understand enums.
1) Public : Whenever we declare a method as public in a class, then via objects of that class that method can be accessible inside the packages or outside the packages and also throughout the program. Also whenever we declare outer class as public then the file name must be same as the class name.
2) Final : Firstly we will see the final keyword in context with classes and then in context with methods. So whenever we declare a class as final using final keyword then no other class can extends(inherit) that class and same for methods, whenever we declare the method or variable as final then that variable or method cannot be overridden.
3) Static : Whenever we declare a method as static using static keyword in a class. then that method can be accessible outside the class without creating the objects of that class. Also when we declare the variable as static in the class and whenever we create objects of that class. Each time while creating the objects the single copy of that variable is created and divided among all objects.
So now that we have discussed a lot about access modifiers, We can move to our main topic that is Enums.
Enum in Java
In java, enum is represented as a special type of class that contains the set of constants (unchangeable variables like final) and it was first introduced in java version 1.5. It is used where all the values are known at compile time (before executing the program). As we declare class using the class keyword and as enum is special type of class, enums are declared using enum keyword instead of class keyword. We can declare a enum inside a class or outside a class but not inside a method. Also if we declare the enum inside the class then we can also apply modifiers like public, private, etc. While declaring enum, first line contains constants (Constants are objects of that enum itself.) and then other things like constructor, methods, variables, etc. All the constants must be written in upper case and also by default they are public, static, final. We have already discussed about public, static, final (access modifiers) at start of the blog.
Following is the simple example of Enum in java.
In above example we created enum of name Size and declared the constants SMALL,MEDIUM and LARGE. Whenever we try to print the contents of a constant then the name of that constant gets printed. As the constant are public, static, final in nature and also the constructor of enum is private so that’s why we cannot instantiate the object of enum. One question that may come in our minds is that if we cannot instantiate enum then what is the need of constructor? The answer to this question is that by using variables in enum we can set that variables. Also it is important to note that class constants are nothing but objects of that enum and the important point to note is that the constructor of enum is private. So whenever we try load the enums then the number of times the enum constructor is called is equal to the number of constants that are defined in the enum i.e. suppose there are 4 constants in the enum then whenever we load the enum then the constructor is called 4 times. This happens because the constants which we define in a enum are basically objects of that enum (enum is nothing but a specialized class) and therefore we see that the constructor is getting called. Now let us see an example of enum using constructor.
In above example we have declared a enum named Color and the constructor which is by default private in nature. Then we are loading the enum from the main method, whenever we load the enum then it will go to enum and as number of constants are 3 it will call the private constructor three times and then it will print the desired output i.e. the constant name which we are expecting.
Methods in Java Enum
So far we have seen the basics of enum i.e. how it is created, how to declare the enum, the constructor, etc. Now we will see the methods of enum. All enums implicitly extends the java.lang.Enum class so the methods written in the java.lang.Enum are available in enum like toString(), valueOf(), etc. Also in java multiple inheritance is not possible so enum cannot extends other classes as it already extends from java.lang.Enum class. Now we will see each method used with enums.
1) Ordinal() : This method returns the position of constant in enum.
e.g. if we write ordinal(YELLOW) then it will return the value as 0. Because the Yellow is declared first, so according to indexing it will return 0, pink will return 1 and so on.
2) compareTo() : This will compare the constants on the basis of ordinal values and it will return the difference between ordinal values of that constants.e
This will return -1, because it will subtract ordinal value of PINK i.e. 1 from ordinal value of YELLOW i.e. 0, so the answer is (0–1) i.e. -1.
3) valueOf() : This method will take a string and returns an enum constant having the same string name. Suppose we write Color.valueOf(“YELLOW”) will return the constant YELLOW declared in enum.
4) values() : This method will returns an array of enum type containing all the enum constants.
E.g. Color constArray = Color.values();
This will store all the constants declared in enum into the constArray.
As stated before, in java enums are nothing but specialized classes and therefore like we have data members and member functions in normal java classes, we can also have variables and methods declared and defined inside enums. See the example given below –
Enum implementing Interface
So as we discussed previously that enum cannot extend from other classes as it already extends from java.lang.Extend. However, it is interesting to note that though enums cannot extend from other classes they can implement interfaces. Following is the example of an enum implementing an interface -
In above example we have declared an interface named tools and enum named education which is implementing the interface tools. In the interface we have a method named display which has to be overridden in the enum as it implementing the interface. And in main function we are trying to invoke the display() method using PEN constant in enum and it gives us the expected output.
Another thing to note is that we can also declare the abstract methods into the enum. Whenever there is any abstract method in enum then all the instance of that enum must implement the abstract methods.
Enums with switch statement
Enums can also used with switch statement. We will understand it by following example.
In above code, we created the enum Coder in which declared constants named BEGINNER, INTERMEDIATE and EXPERT. Then in main method we are created reference of enum and we are loading BEGINNER constants in that reference and we are passing it as choice in switch statement where we declared cases for all constants. Then the case with choice as BEGINNER is getting executed because we are passing reference of enum in which we loaded BEGINNER constants. So it will simply log the output as beginner.
Enum in java vs. Enum in C++
Enums are kind of class in java whereas in C/C++ enums are constant , that can be used in indexed expressions and as operands. Also C/C++ enumerations provide an alternative to the #define pre-processor directive. Enums in Java are objects — they can have methods. Enums in C/C++ enums are plain Integers. The enums are type-safe means that an enum has its own namespace, we can’t assign any other value other than specified in enum constants in C/C++ enums are not type-safe. As we talked constants in enum in C++ by default hold the integers and enums in java hold the constant name as string. Enums in C++, by default hold integers so we can use them in place of #define which is used for declaring variables which cannot be reinitialized.
Applications of enum
Java enums are widely used in different Java applications such as spring application for mapping all the fields such as code and description fields. Another application of enum types is to represent categories of objects. It can be also used to know the download status of a file which gets downloaded from a base station.
- Rahul Kanade
- Shubham Kasar
- Shruti Kokate
- Adarsh Londhe
- Kshitij Magare