Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

Java Enumeration example

In this tutorial we will discuss about enumerations in Java. The Java programming language contains the enum keyword, which represents a special data type that enables for a variable to belong to a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

The values defined inside an enum are constants and shall be typed in uppercase letters. Also, these values are implicitly static and final and cannot be changed, after their declaration. If an enum is a member of a class, then it is implicitly defined as static. Finally, you should use an enum, when you need to define and represent a fixed set of constants.

The most common example of a simple enum includes the days of the week, as shown below:

public enum Day {

Enums in Java are type-safe and thus, the value of an enum variable must be one of the predefined values of the enum itself. Also, enums have their own namespace and they can be used in a switch statement, in the same way as integers.

Furthermore, enums in Java are considered to be reference types, like class or Interface and thus, a programmer can define constructor, methods and variables, inside an enum.

In addition, a programmer can specify values of enum constants at their creation time, as shown in the example below:

public enum Day {

However, in order for this approach to function properly, a private member variable and a private constructor must be defined, as shown below:

private int value;
private Day(int value) {
	this.value = value;

In order to retrieve the value of each constant of the enum, you can define a public method inside the enum:

public int getValue() {
	return this.value;

As we already mentioned, the values inside an enum are constants and thus, you can use them in comparisons using the equals or compareTo methods.

The Java Compiler automatically generates a static method for each enum, called values. This method returns an array of all constants defined inside the enum:

//Printing all constants of an enum.
for(Day day: Day.values())

Notice that the values are returned in the same order as they were initially defined.

Methods: name, valueOf and toString

An enum contains the name and valueOf methods. The valueOf method can be used to map from a name to the corresponding enum constant, while the name method returns the name of an enum’s constant, exactly as declared in its declaration. Also, an enum can override the toString method, just like any other Java class. A simple example is shown below:

public String toString() {
	switch(this) {
		case FRIDAY:
			return "Friday: " + value;
		case MONDAY:
			return "Monday: " + value;
		case SATURDAY:
			return "Saturday: " + value;
		case SUNDAY:
			return "Sunday: " + value;
		case THURSDAY:
			return "Thursday: " + value;
		case TUESDAY:
			return "Tuesday: " + value;
			return "Wednesday: " + value;
			return null;

An enum in Java can implement an Interface or extend a class, just like any other normal class. Also, an enum implicitly implements both the Serializable and Comparable interfaces.

Finally, you can define abstract methods inside an enum in Java. Each constant of the enum implements each abstract method independently. For example:

public enum Car {
		public int getPrice() {
			return 25000;
		public int getPrice() {
			return 30000;
	BMW {
		public int getPrice() {
			return 20000;
	public abstract int getPrice();

The EnumMap and EnumSet Collections

An EnumMap is a specialized Map implementation. All of the keys in an EnumMap must come from a single enum type that is specified, explicitly or implicitly, when the map is created. EnumMaps are represented internally as arrays. Also, EnumMaps are maintained in the natural order of their keys.

A sample example that uses an EnumMap is shown below:

import java.util.EnumMap;

public class EnumMapExample {
	public static void main(String[] args) {
		// Create an EnumMap that contains all constants of the Car enum.
		EnumMap cars = new EnumMap(Car.class);
		// Put some values in the EnumMap.
		cars.put(Car.BMW, Car.BMW.getPrice());
		cars.put(Car.AUDI, Car.AUDI.getPrice());
		cars.put(Car.MERCEDES, Car.MERCEDES.getPrice());
		// Print the values of an EnumMap.
		for(Car c: cars.keySet())
		// Remove a Day object.
		System.out.println("After removing Car.BMW, size: " + cars.size());
		// Insert a Day object.
		cars.put(Car.valueOf("BMW"), Car.BMW.getPrice());
		System.out.println("Size is now: " + cars.size());

A sample execution is shown below:

After removing Car.BMW, size: 2
Size is now: 3

An EnumSet is a specialized Set implementation. All of the elements in an EnumSet must come from a single enum type that is specified, explicitly or implicitly, when the set is created. EnumSet are represented internally as bit vectors. Also, an iterator traverses the elements in their natural order.

A sample example that uses an EnumSet is shown below:

import java.util.EnumSet;

public class EnumSetExample {
	public static void main(String[] args) {
		// Create an EnumSet that contains all days of the week.
		EnumSet week = EnumSet.allOf(Day.class);
		// Print the values of an EnumSet.
		for(Day d: week)
		// Remove a Day object.
		System.out.println("After removing Day.FRIDAY, size: " + week.size());
		// Insert a Day object.
		System.out.println("Size is now: " + week.size());

A sample execution is shown below:

After removing Day.FRIDAY, size: 6
Size is now: 7

Download the Eclipse Project

The Eclipse project of this example:

This was a tutorial about enums in Java.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books