The perfect place for easy learning...

Java Programming

×

Topics List


Enum in Java





In java, an Enumeration is a list of named constants. The enum concept was introduced in Java SE 5 version. The enum in Java programming the concept of enumeration is greatly expanded with lot more new features compared to the other languages like C, and C++.

In java, the enumeration concept was defined based on the class concept. When we create an enum in java, it converts into a class type. This concept enables the java enum to have constructors, methods, and instance variables.

All the constants of an enum are public, static, and final. As they are static, we can access directly using enum name.

The main objective of enum is to define our own data types in Java, and they are said to be enumeration data types.

Creating enum in Java

To create enum in Java, we use the keyword enum. The syntax for creating enum is similar to that of class.

In java, an enum can be defined outside a class, inside a class, but not inside a method.

Let's look at the following example program for creating a basic enum.

Example
enum WeekDay{
	MONDAY, TUESDAY, WEDNESSDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}


public class EnumerationExample {

	public static void main(String[] args) {

		WeekDay day = WeekDay.FRIDAY;
		
		System.out.println("Today is " + day);
		
		System.out.println("\nAll WeekDays: ");
		for(WeekDay d:WeekDay.values())
			System.out.println(d);

	}

}

When we run the above program, it produce the following output.

Enum in java

🔔 Every enum is converted to a class that extends the built-in class Enum.

🔔 Every constant of an enum is defined as an object.

🔔 As an enum represents a class, it can have methods, constructors. It also gets a few extra methods from the Enum class, and one of them is the values() method.

Constructors in Java enum

In a java programming language, an enum can have both the type of constructors default and parameterized. The execution of constructor depends on the constants we defined in the enum. For every constant in an enum, the constructor is executed.

If an enum has a parameterized constructor, the parameter value should be passed when we define constant.

Let's look at the following example program for illustrating constructors in enum.

Example
enum WeekDay{
	MONDAY, TUESDAY, WEDNESSDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY("Holiday");
	
	String msg;
	
	WeekDay(){
		System.out.println("Default constructor!");
	}
	
	WeekDay(String str){
		System.out.println("Parameterized constructor!");
		msg = str;
	}
}


public class EnumerationExample {
	
	public static void main(String[] args) {

		WeekDay day = WeekDay.SUNDAY;
		
		System.out.println("\nToday is " + day + " and its " + day.msg);

	}

}

When we run the above program, it produce the following output.

Enum in java

In the above example, the constant SUNDAY is created by calling the parameterized constructor, other constants created by calling default constructor.

Methods in Java enum

In a java programming language, an enum can have methods. These methods are similar to the methods of a class. All the methods defined in an enum can be used with all the constants defined by the same enum.

Let's look at the following example program for illustrating methods in enum.

Example
enum WeekDay{
	MONDAY, TUESDAY, WEDNESSDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY("Holiday");
	
	String msg;
	
	WeekDay(){
		System.out.println("Default constructor!");
	}
	
	WeekDay(String str){
		System.out.println("Parameterized constructor!");
		msg = str;
	}
	
	void printMessage() {
		System.out.println("Today is also " + msg);
	}
}


public class EnumerationExample {
	
	public static void main(String[] args) {

		WeekDay day = WeekDay.SUNDAY;
		
		System.out.println("\nToday is " + day);
		day.printMessage();
	}
}

When we run the above program, it produce the following output.

Enum in java

🔔 In java, enum can not extend another enum and a class.

🔔 In java, enum can implement interfaces.

🔔 In java, enum does not allow to create an object of it.

🔔 In java, every enum extends a built-in class Enum by default.