AppDividend
Latest Code Tutorials

Java Enum: The Complete Guide

Java is an Object Oriented Programming language. Therefore, enum in Java is more potent than enum in C/C++. We can add methods, variables, and constructor to it.

Key Points in Enum

  1. enum improve type safety.
  2. enum can be easily used in the switch.
  3. enum can have fields, constructors, and methods.
  4. enum can implement many interfaces but cannot extend any class because it internally extends the Enum class.
  5. enum can be traversed.

Java Enum

Enum in Java represents a group of named constants in the programming. The enumeration is a particular class representing a group of constants. All possible values in the Enums are used at Compile time. Therefore, we can represent an Enum class with the help of the enum data type.

For example, the four suits in the deck of playing cards may be four enumerators named Club, Diamond, Heart, and Spade, belonging to the enumerated type named Suit.

Other examples include the enumerated natural types like the planets, days of the week, colors, directions, etc. Finally, enums are unchangeable variables, like final variables.

Let’s see how to create declare an enum.

Syntax of Enum

enum enum_name
{
     Constant list/ methods
}

See the following Test.java file.

enum WeekDays
{ 
   sunday, monday, tuesday, wednesday, thursday, friday, saturday
}

class Test
{
  public static void main(String []er)
  {
    WeekDays wd; //wk is an enumeration variable of type WeekDays
    wd = WeekDays.sunday; //wk can be assigned only the constants defined under enum type Weekdays
    System.out.println("Today is "+wd);
  }
}

In the above example, Sunday, Monday…these are called Enum constant. These are public, static, and final by default.

Can we create an instance of enum by a new keyword?

No, because it contains the private constructors only.

Can we have an abstract method in the enum?

Yes, of course! We can have abstract methods and can provide the implementation of these methods.

Variables of enumeration can be declared without a new keyword.

Variables of Enumeration type can have only enumeration constants as a value. Therefore, we define an enum variable as the following syntax.

         enum_variable = enum_type.enum_constant;

See the output.

Java Enum Tutorial With Example

Now let’s see how to use the switch case in the enumeration.

See the following code.

enum WeekDays
{ 
	sunday, monday, tuesday, wednesday, thursday, friday, saturday
}

class Switch 
{
 public static void main(String []er)
 {
    WeekDays wd; 
    wd = WeekDays.monday; 
    switch(wd)
    {
	case sunday:
	    System.out.println("Today is "+wd.sunday);
		break;
	case monday:
		System.out.println("Today is "+wd.monday);
		break;
		
	case tuesday:
		System.out.println("Today is "+wd.tuesday);
		break;
	case wednesday:
		System.out.println("Today is "+wd.wednesday);
		break;
	case thursday:
		System.out.println("Today is "+wd.thursday);
		break;
	case friday:
		System.out.println("Today is "+wd.friday);
		break;
	case saturday:
		System.out.println("Today is "+wd.saturday);
		break;
     } 
   }
 }

See the output.

Switch case in the enumeration

Values() and valueOf() method

All the enumerations are predefined methods values() and valueOf().

What is the main purpose of the values() method in the enum?

The java compiler internally adds the values() function to create the enum. The values() method returns the array containing all the values of an enum.

The values() method returns the array of enum-type containing all the enumeration constants.

Syntax for values() method is following.

public static enum-type [] values();

The valueOf() method returns the constant enumeration value, which is equal to a string passed in as an argument while calling this method.

See the following syntax

public static enum-type[] valueOf(String s);

Let’s see an example of these two methods.

enum WeekDays
{ 
	sunday, monday, tuesday, wednesday, thursday, friday, saturday
}

class Method
{
 public static void main(String []er)
 {
    WeekDays wd;  
    System.out.println("Constant of enum type weekdays are :");
    WeekDays arr[]=WeekDays.values();
    for(WeekDays a: arr)
    {
	  System.out.print(" "+a);
    }
    wd=WeekDays.valueOf("sunday");
    System.out.println("\nToday is "+wd);
 }
}

See the following output.

Values() and valueOf() method

Now we will understand the full program containing the constructor methods in Enumeration.

See the below program and analyze the output.

enum WeekDays
{ 
	sunday(1), monday(2), tuesday(3), wednesday(4), thursday(5), friday(6), saturday(7);
	private int day;
	int getday()
	{
		return day;
	}
	 WeekDays(int day)
	{
		this.day=day;
	}
}

class Final
{
   public static void main(String []er)
   {
      WeekDays wd; 
      System.out.println("Sunday is the "+WeekDays.sunday.getday()+" day of week");
   }
}

See the below output.

valueOf() method

Difference between Enums and Classes

The enum can, just like a class, have attributes and methods.

The major difference is that enum constants are public, static, final, and unchangeable, which cannot be overridden.

The enum cannot be used to create objects, and it can not extend other classes, but it can implement interfaces.

So, that is it for Enums in Java.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.