Enum

Enum

In this tutorial, we are going to discuss about the Enum in java. An enum is a group of named constants. enum can be used for defining user defined data types. Enums, short for “enumerations,” are a special type in programming languages that define a set of named constants, typically representing a finite set of possible values for a particular variable.

Enums are useful for improving code readability, maintainability, and type safety by providing a way to represent a fixed set of related constants in a concise and expressive manner.

Enum

E.g

enum Month {
	JANUARY, 
	FEBRUARY, 
	MARCH, 
	APRIL, 
	MAY, 
	JUNE, 
	JULY, 
	AUGUST, 
	SEPTEMBER, 
	OCTOBER, 
	NOVEMBER, 
	DECEMBER;
}

enum concept has introduced in 1.5 version. When compared with old languages enum, the java enum is more powerful because, in java enum we are allowed to take instance members, constructors ..etc which may not be possible in old languages enum.

Every constant inside enum is implicitly public static and final by default. And we can access enum constants by using enum name.

package com.ashok.enums;
/**
 * 
 * @author ashok.mariyala
 *
 */
enum Month {
   JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
   JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}

public class MyEnum {
   public static void main(String[] args) {
      Month mon = Month.DECEMBER;
      System.out.println(mon);
   }
}

Output

DECEMBER

Because By Default enum constants are public static final. So in method we can’t declare any static members. If we are declaring the enum outside the class the allowed modifiers are public, , and strictfp. If we are declaring enum with in a class the allowed modifiers are public, private, , strictfp and static.

Enum Vs Inheritance

Every enum in java should be the direct child class of java.lang.enum. As every enum is already extending java.lang.enum there is no chance of extending any other enum. Hence inheritance concept is not applicable to the enum. This is the reason abstract and final modifiers are not applicable for enum. java.lang.enum is an abstract class and it is direct child class of Object. It implements comparable and serializble interfaces. For java enums the required functionalities defined in this class only.

Enum Vs switch

We can pass enumtype as an argument to switch statement. From 1.5 versions on words the following is the list of valid argument to the switch.

Capture 29
package com.ashok.enums;

/**
 * 
 * @author ashok.mariyala
 *
 */
enum Beer {
   KF, KO, RC, FO;
}

public class MyEnum {
   public static void main(String args[]) {
      Beer b1 = Beer.RC;
      switch (b1) {
      case KF:
         System.out.println("KF is childrens brand");
         break;
      case KO:
         System.out.println("KO is too lite");
         break;
      case RC:
         System.out.println("RC is too hot");
         break;
      case FO:
         System.out.println("Buy one get one");
         break;
      default:
         System.out.println("Other brands are not good");
      }
   }
}

Output

RC is too hot

Note

If we are passing enum constants as the switch argument all the case labels should be valid enum constants otherwise we will get compiler time error.

values() method

Every enum implicitly contains values method to list all it’s constants.

package com.ashok.enums;
/**
 * 
 * @author ashok.mariyala
 *
 */
enum Months {
   JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
     JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}

public class MyEnum {
   public static void main(String[] args) {
      Months[] mons = Months.values();
      for(Months mon : mons) {
         System.out.println(mon);
      }
   }
}

Output

JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER
ordinal() method

The position of enum constant is important and it is described with ordinal() method we can find ordinal value of an enum constant by using following method.

package com.ashok.enums;

/**
 * 
 * @author ashok.mariyala
 *
 */
enum Months {
   JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
     JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}

public class MyEnum {
   public static void main(String[] args) {
      Months[] mons = Months.values();
      for(Months mon : mons) {
         System.out.println(mon + " --> " +mon.ordinal());
      }
   }
}

Output

JANUARY --> 0
FEBRUARY --> 1
MARCH --> 2
APRIL --> 3
MAY --> 4
JUNE --> 5
JULY --> 6
AUGUST --> 7
SEPTEMBER --> 8
OCTOBER --> 9
NOVEMBER --> 10
DECEMBER --> 11
Speciality of java enum

A java enum can contain constructors, instance members, static members etc. in addition to constants which may not be possible in the old languages enum.

package com.ashok.enums;

/**
 * 
 * @author ashok.mariyala
 *
 */
enum Beer {
   KF(100), KO(120), RC(150), FO;
   int price;

   Beer(int price) {
      this.price = price;
      System.out.println("Inside Constructor");
   }

   Beer() {
      this.price = 130;
      System.out.println("Inside No Argument Constructor");
   }

   public int getPrice() {
      return price;
   }
}

public class MyEnum {
   public static void main(String arg[]) {
      Beer[] b = Beer.values();
      for (Beer b1 : b) {
         System.out.println(b1 + " --> " + b1.getPrice());
      }
   }
}

Output

Inside Constructor
Inside Constructor
Inside Constructor
Inside No Argument Constructor
KF --> 100
KO --> 120
RC --> 150
FO --> 130

An enum can contain constructors also and these will execute at the time of enum class loading. Programmer is not responsible to call constructors explicitly. With in enum we can’t take abstract methods. If the enum contains any thing other than constants(like instance variables, static variables.. etc) then the list of constants should end with semicolon.

enum Month {
   JAN,FEB,MAR ; // ; Mandatory
   int num;
}

enum Month {
   JAN,FEB,MAR ; // ; Optional
}

 If the enum contains anything else other than constants then the list of constants should be the first line.

enum Month {
   int num; // C.E
   JAN,FEB,MAR ;
}

enum in java internally implemented as class concept only. We are allowed to declare main() method in enum and we can involve directly from the command prompt.

package com.ashok.enums;
/**
 * 
 * @author ashok.mariyala
 *
 */
enum MyEnum {
   JAN, FEB, MAR;
   public static void main(String arg[]) {
      System.out.println("Welcome to Waytoeasylearn");
   }
}

Output

Welcome to Waytoeasylearn

Note

Between enum constants, we can apply equality operators (== & !=) and we can’t apply relational operators.

enum Vs Enum Vs Enumeration

1. enum

enum is a keyword which can be used to define a group of named constants.

2. Enum

It is a class present in java.lang package. Every enum in java is the direct child class of this class. Hence this Enum class acts as base class for all java enum’s .

3. Enumeration

It is a interface present in java.util package. We can use Enumeration to get the objects one by one from the Collections.

That’s all about the enum in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Enum
Scroll to top