This post will delve into Java Enumerations or Java Enums. A Java Enum or Enumeration is a specific type of Java class. Enums in Java represent classes with a predetermined set of constants. Java enum constants are inherently static and final. This feature was introduced with JDK 1.5. Enums serve the purpose of defining custom data types, akin to classes. In Java, Enums can include variables, methods, and constructors. The fundamental objective of enums is to establish our distinct data types.

Sample example of Java Enum

The subsequent example illustrates the utilization of Java Enum. In this instance, all weekdays are incorporated as enum constants. Attempting to represent the seven weekdays using primitive data types is impractical, prompting the need to declare weekdays as a Java Enum. The Enum consists of seven weekdays, spanning from Sunday to Saturday. The main method employs Java Enum to print the current weekday.

JavaTest.java

package com.test;

public class JavaTest {

	enum WEEKDAY {
		SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
	}

	public static void main(String[] args) {
		System.out.println(WEEKDAY.MONDAY);
		System.out.println(WEEKDAY.SATURDAY);
	}

}

Java Enum Data type

When there is a requirement for a predefined set of constant values known at compile time, employing an Enum type is beneficial. Examples include days of the week and months of the year.

Java Enum is distinctive as a class in Java, characterized by the following features:

  • The declared enum constants in the java enum cannot be overridden.
  • The java enum does not support the creation of Enum class objects.
  • Java enums are unable to extend other Java classes.
  • Java enum, like any other Java class, can implement interfaces.

How to initialize Java Enum with values

By default, Java enum constants are assigned integer numbers starting from 0. However, custom values can be assigned to Java enums when declaring the enum data type. These values are specified after the enum constant in parentheses. To read and assign these values to the enum class object, a constructor method for the enum should be added. Each enum constant is essentially an enum object that stores the values specified. The following example demonstrates how to declare enum constants with custom values.

JavaTest.java

package com.test;

public class JavaTest {

	enum WEEKDAY {
		SUNDAY(10), MONDAY(20), TUESDAY(30), WEDNESDAY(40), THURSDAY(50), FRIDAY(60), SATURDAY(70);
		
		private int value = 10;
		private WEEKDAY(int value) {
			this.value=value;
		}
	}

	public static void main(String[] args) {
		System.out.println(WEEKDAY.MONDAY.value);
		System.out.println(WEEKDAY.SATURDAY);
	}

}

Iterate all Java Enum Values

The Java enum provides access to all its constants through the values() method. This method returns an array of enum constants. To print all of these constants, you can iterate through the array. The following example demonstrates how to use a for loop to iterate through all the constants in a Java enum.

package com.test;

public class JavaTest {

	enum WEEKDAY {
		SUNDAY(10), MONDAY(20), TUESDAY(30), WEDNESDAY(40), THURSDAY(50), FRIDAY(60), SATURDAY(70);
		
		private int value = 10;
		private WEEKDAY(int value) {
			this.value=value;
		}
	}

	public static void main(String[] args) {
		for(WEEKDAY day: WEEKDAY.values()) {
			System.out.println(day+" "+day.value);
		}
	}

}

How to use Abstract Method in Enum

Java enums allow the definition of abstract methods within them. All constants declared in the Java enum must implement these abstract methods. The abstract method declaration should be of the same data type as the Java enum. The following example demonstrates how to create and use an abstract method within a Java enum.

package com.test;

public class JavaTest {

	enum WEEKDAY {
		SUNDAY { @Override public String getShortForm() {return "Sun"; } }, 
		MONDAY { @Override public String getShortForm() {return "Mon"; } }, 
		TUESDAY { @Override public String getShortForm() {return "Tue"; } },
		WEDNESDAY { @Override public String getShortForm() {return "Wed"; } }, 
		THURSDAY { @Override public String getShortForm() {return "Thu"; } }, 
		FRIDAY { @Override public String getShortForm() {return "Fri"; } }, 
		SATURDAY { @Override public String getShortForm() {return "Sat"; } };
		
		public abstract String getShortForm();
	}

	public static void main(String[] args) {
		for(WEEKDAY day: WEEKDAY.values()) {
			System.out.println(day+" "+day.getShortForm());
		}
	}

}

How to use interface in Java Enum

Interfaces can be implemented in the Java enum data type. The methods declared in the interface must be implemented either in the Java enum class or in each Java enum constant declaration. For the Java enum implemented using an interface to be functional, all enum constants must have access to the interface methods. The following example demonstrates how to use a Java enum that implements a Java interface.

package com.test;

public class JavaTest {

	public interface IWeekDay {
		public String getShortForm();
	}
	
	enum WEEKDAY implements IWeekDay {
		SUNDAY {  public String getShortForm() {return "Sun"; } }, 
		MONDAY { public String getShortForm() {return "Mon"; } }, 
		TUESDAY { public String getShortForm() {return "Tue"; } },
		WEDNESDAY { public String getShortForm() {return "Wed"; } }, 
		THURSDAY { public String getShortForm() {return "Thu"; } }, 
		FRIDAY { public String getShortForm() {return "Fri"; } }, 
		SATURDAY { public String getShortForm() {return "Sat"; } };
	}

	public static void main(String[] args) {
		for(WEEKDAY day: WEEKDAY.values()) {
			System.out.println(day+" "+day.getShortForm());
		}
	}

}

Leave a Reply