AppDividend
Latest Code Tutorials

Java Enum Tutorial With Example | Enumeration Class in Java

0

Java Enum Tutorial With Example | Enumeration Class in Java is today’s topic. Java is an Object Oriented Programming language. In Java, an enumeration is a particular class that represents a group of constants. All possible values in the Enums are used at Compile time. In Java, we represent the Enum class with the help of enum data type. Enum in Java more powerful than enum in C/C++. We can add methods, variables, and constructor to it.

Key Points in Enum

  1. enum improves 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 Tutorial With Example

Enumerations represent a group of named constants in the programming. 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 natural enumerated types like the planets, days of the week, colors, directions, etc. 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 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 the 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. We define an enum variable as the following syntax.

         enum_variable = enum_type.enum_constant;

The output is following.

Java Enum Tutorial With Example

 

Now let’s see how to use 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 following 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 values() method in the enum?

The java compiler internally adds the values() function when it creates 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 in it.

Syntax for values() method is following.

public static enum-type [] values();

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

The syntax is following.

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 are going to 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, which have attributes and methods.

The major difference is that enum constants are public, static, and final and it is unchangeable, which means it 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.

Finally, Java Enum Tutorial With Example | Enumeration Class in Java article is over.

Leave A Reply

Your email address will not be published.

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