java enums tutorial: Best 10 examples of enum in java

Enums in java:-

Enum is one of the cool language featured introduced as part of java 5 version.
enum values are constants meaning which can not be changed. These constants are specified using upper case letters as per java coding specifications

Enum is introduced in java 5 version,Before java 5 was introduced, we have to write our own constants to handle enum functionality as follows
public class MonthsConstants{
public static final String JAN="1";
public static final String FEB="2";
public class MonthsConstantsTest
{ int monthsCode; //can assign any value like 1,2 or 100

You have to handle all your constants yourself and causes error prone.
These values are valid type for compile time only and gives error if wrong type is assigned to these values. Moreover you can assign any values
if we want to add new constants, it would be more work is required for maintenance of your code.Because of all these enum class is introduced.

By default, enum implements java.lang.Enum which implements and java.lang.Comparable interfaces.

java enum API provides different methods like toString(),name() as well as valueOf() methods

I am listing down the best enum examples in java
1.How to define enum class?
This is one of the way to define enum in java
public enum Months{
This is going to create in java class and enum class name. Please note that enum constants has semicolon at the end of constant defination.

2.How to define enum inside an other class:-
public class MonthsClass{
public enum Months{
Months monthsNames;
In the above example, enum class is created in inside the class, please note that semicolo is not required after defining enum constants.

3.How to create enum object?:-
MonthsClass monthsObject=new MonthsClass();
we can create enum objects using new operator. and also we can attach constants to the enum class
4. how to define enum constructor :-
public enum Months{
private int monthCode
Months(int monthCode){
public int value(){
return monthCode;
enum constructor in enum are private, meaning outside classes should not able create enum objects creation.

5. How to compare Enum objects in java:-
normally we can compare objects using == and equals methods
enum constants can also be compared using the same.
if(Months.JAN==Months.FEB){return true else return false;}
we can also use equals method to compare the enum objects of same type.

6.iterate enum constants:-
enum class provides values() method for iteration which returns list of enum constants in the order of constants creation.

for(Months months:Months.values()){
System.out.print(" "+months);
and output is:-
7.How to convert enum to integer ?:-
We have encountered in our programming to convert enum constant to integer.
As defined enum declartion in steps #4
Months months=Months.Jan;
Integer int=new Integer(months.value());
value method in enum provides the value for the enum constants.

8.How to convert integer to enum constant?:-
we have to define the following method in enum class for returning the Enum for the given input as integer
static Months getMonths( int monthCode ) throws Exception
for (Months months : Months.values())
if (months.value() == monthCode)
return months;
Hope this solves the conversion to enum by providing integer as input to the above method

9. How to convert String to enum object?
enum class provides valueOf(String )method to pass the string as an argument, which return enum constants
Months month=Months.valueOf("JAN");
Months month=Months.valueOf("abc");
For first line in the above code,month object prints the string value as jan

second, valid string is not passed to enum class, it throws java.lang.IllegalArgumentException with message No enum constants for your class. to use enum class in switch condition:-

one of the coolest feature of enum is we can use enum objects in switch case as like a primitive type
Months month=Months.JAN;
 case JAN:
 case FEB:

Advantages of Enum class:-

1.By default enum classes are implementing serialization, and overidng equals and hashcode methods so no need for explicit serialization implementation
2.With usage of Enum class, EnumSet and EnumMap are introduced,collection of enums operations can be achieved
3. Maintenance of code using enums is very easy

Disadvantages of Enum class:-
enum class is extending java.lang.Enum, the classes in java.lang are final,so enum classes can not be subclassed.

Hope you understand enum examples in java.

Please share your comments that how you are using the enum class in your development/

1 comment:

Note: only a member of this blog may post a comment.