enum keyword in java

The ” enum ” keyword is used to define a fixed set of constants.

enum keyword is introduced in Java 5.

It can be used for set of status in the application, set of some constant names in the project etc.

The general syntax for an enum type is as follows:

  1. enum <enum_name> {
  2.  
  3.     <enum_constant_1>,
  4.  
  5.     <enum_constant_2>,
  6.  
  7.     ...
  8.  
  9.     <enum_constant_n>
  10.  
  11.     <enum_constructor>
  12.  
  13.     // other variables & methods if required
  14. }
enum <enum_name> {

    <enum_constant_1>,

    <enum_constant_2>,

    ...

    <enum_constant_n>

    <enum_constructor>

    // other variables & methods if required
}

Example
  1. public enum status {
  2.  
  3.     CREATED,INPROGRESS,COMPLETED
  4. }
public enum status {

    CREATED,INPROGRESS,COMPLETED
}


An enum type containing constructor, method and variable:

  1. public enum Priority {
  2.  
  3.      LOW(4),NORMAL(3), HIGH(2), SEVERE(1);
  4.  
  5.      priority(int priorityNumber) {
  6.  
  7.         this.priorityNumber = priorityNumber;
  8.     }
  9.  
  10.     private int priorityNumber;
  11.  
  12.     public int getPriorityNumber() {
  13.  
  14.         return this.priorityNumber;
  15.     }
  16. }
public enum Priority {

     LOW(4),NORMAL(3), HIGH(2), SEVERE(1);

     priority(int priorityNumber) {

        this.priorityNumber = priorityNumber;
    }
 
    private int priorityNumber;
 
    public int getPriorityNumber() {

        return this.priorityNumber;
    }
}


Enum can be iterated as below

  1. for (Priority priority : Priority.values()) {
  2.  
  3.     System.out.println(priority);
  4. }
for (Priority priority : Priority.values()) { 

    System.out.println(priority); 
}


Points to remember about Java Enum

enum improves type safety

enum can be used in switch

enum can be traversed using enumerations

enum can have fields, constructors and methods as explained above

enum may implement interfaces but can’t extend any class because it internally extends Enum class


Rules to follow for Enum types


An enum constant specifies an instance of the enum type.

An enum constant can be optionally followed by a list of arguments and a class body.The class body is an anonymous class

An enum type can not be declared as abstract or final.

The Enum is the direct superclass of an enum type.

An enum type can be only declared inside class level, same as class level or in a separate source file.

enum can not be declared inside a method or an inner class.

An enum type can have constructors, methods and variables just like a regular Java class as shown in the above example.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on