Java Enum tutorial with examples

Java Enum is special data type which represents list of constants values. It is a special type of java class.  It can contain constant, methods and constructors  etc.
Lets see example to understand better
Let’s say we get three types of issues which require different SLA to get it fixed. Usually they are level 1,level 2 and level 3 severity. So it will represent in term of enums as follows.
Here

level variable is type of Serverity which is enum type. You can assign only 3 values (Level_1,Level_2 and Level_3) to it.If you assign any other value, it will give you compile time error.Enum in java are type safe and has its own name space

Now you must be wondering why you can’t use class over here,  why you require special data type called enum.
Lets understand it with the help of example:
Create enum as SeverityEnum.java
Create class as SeverityClass.java

Create issue.java. It will use above enum and class.

Create EnumMain.java .

When you run above program, you will get following output:

The main  reason for it is type safety . You won’t be able to put any other value with enums but if you use class instead of enum, it may be more error prone as you may assign invalid value to it.

Lets understand more about Enum.

We can see structure of enum using a debug point.
put debug point at line 21 and right click on project->debug as->java application. Program will stop execution at line 21 then right click on level then select watch.You will be able to see structure as below.
Java Enum structure

So it has many methods and two of them are :
name() : it returns you enum constant. For above enum level, its value is LEVEL_2.
ordinal() : It returns you position in enum declaration. This method is not generally used by programmers, it is used mostly in enum based data structures such as EnumSet.Constructor:
protected Enum(String name, int ordinal)
This is only constructor present by default in enum. This can not be called by program. This is called internally when you declare the enum.I am summarising important points on enum based on their nature.

Constructor:

  • Enum can have constructors and can be overloaded.
  • Enum constructor can never be invoked directly, it will be implicitly called when enum is initialized.
  • You also can provide value to enum constants but you need to create member variable and constructor for that

Methods:

  • You can declare methods in enum.
  • You can also declare abstract methods in enum and while declaring constants,  you need to override it.
Java Enum example with description

Declaration :

    • You can not create instance of enum with new operation as it does not support public constructor and it make sense too as you want fixed values in Enum. If new operator would have been allowed, then you might able to add more values.
    • Enum constants are implicitly static and final.  You can not change its value after creation.
    • You can not extend any other class to enums as enum implicitly extends Enum class and java does not allow multiple inheritance.

You can not extends any other class in enum

    • Enum can be defined inside or outside of a class but can not be defined in method.
    • Enum declared outside class can not be declared as static,  abstract,  private,  protected or final.
    • Enum can implement an interface.

Enum interface example

  • Semi colon declared at the end of enum is optional.

Comparison and Switch case:

  • You can compare values of enum using ‘==’  operator.
  • You can use enum in switch case too.

Example:

create Enum class named Severity.java

Create issue.java which will used above enum class.

create enumMain.java to run the code:

When you run above code, you will get following output:

Please go through  core java interview questions for more interview questions.

Was this post helpful?

Leave a Reply

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