Apr 242017

ADM

Enum examples in Java

  • 24 April 2017
  • ADM

 

 

 

Enum examples in Java - images/logos/java.jpg

 

A Java Enum is a Java type used to define collections of constants. in other words, a Java enum type is a special kind of Java class. An enum can contain constants, methods etc. but enum types cannot extend another class. The Enum type was added in Java 5.

Here are a few examples how to use Enum objects in Java.

Simple Enum

package com.admfactory.enums;

public enum OrderStatus {
    CREATED, ASSIGNED, INPROGRESS, FINISHED, DELIVERED, CLOSED;
}

Usage

package com.admfactory.enums;

public class EnumExample1 {
    public static void main(String[] args) {
	System.out.println("Basic enum example");

	System.out.println();
	System.out.println("List all values from enum");
	for (OrderStatus status : OrderStatus.values()) {
	    System.out.println(status);
	}

	System.out.println();
	System.out.println("Get one value from enum");
	System.out.println(OrderStatus.DELIVERED);

	System.out.println("Compare enum with ==");
	OrderStatus status = OrderStatus.ASSIGNED;
	if (status == OrderStatus.ASSIGNED) {
	    System.out.println("The order has been assigned.");
	}

	System.out.println();
	System.out.println("Convert a String to Enum object.");
	OrderStatus status2 = OrderStatus.valueOf("CLOSED");
	System.out.println(status2);
    }
}

Output

Basic enum example

List all values from enum
CREATED
ASSIGNED
INPROGRESS
FINISHED
DELIVERED
CLOSED

Get one value from enum
DELIVERED
Compare enum with ==
The order has been assigned.

Convert a String to Enum object.
CLOSED

Enum with field and method

If a Java enum contains fields and methods, the definition of fields and methods must be always added after the list of constants in the enum. Also, the list of enum constants must be terminated by a semicolon;

package com.admfactory.enums;

public enum OrderStatus {
    CREATED("created"), 
    ASSIGNED("assigned"), 
    INPROGRESS("inprogress"), 
    FINISHED("finished"), 
    DELIVERED("delivered"), 
    CLOSED("closed");

    /**
     * Field to store the enum value
     */
    private final String status;

    OrderStatus(String s) {
	this.status = s;
    }

    /**
     * Method to get the status value
     * 
     * @return
     */
    public String getStatus() {
	return this.status;
    }
}

Usage

If you try to convert a string to Enum object using the value, you will get an error.

OrderStatus status2 = OrderStatus.valueOf(OrderStatus.class, "closed");

the error is:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.admfactory.enums.OrderStatus.closed
	at java.lang.Enum.valueOf(Unknown Source)
	at com.admfactory.enums.EnumExample2.main(EnumExample2.java:25)

To overcome this will add a new method in the Enum to deal with it.

    public static OrderStatus fromValue(String status) {
	for (OrderStatus st : values()) {
	    if (status.equals(st.getStatus())) {
		return st;
	    }
	}
	return null;
    }

Full Example

package com.admfactory.enums;

public class EnumExample2 {
    public static void main(String[] args) {
	System.out.println("Enum with field and method example");

	System.out.println();
	System.out.println("List all values from enum");
	for (OrderStatus status : OrderStatus.values()) {
	    System.out.println(status + ": " + status.getStatus());
	}

	System.out.println();
	System.out.println("Get one value from enum");
	System.out.println(OrderStatus.DELIVERED);

	System.out.println("Compare enum with ==");
	OrderStatus status = OrderStatus.ASSIGNED;
	if (status == OrderStatus.ASSIGNED) {
	    System.out.println("The order has been assigned.");
	}

	System.out.println();
	System.out.println("Convert a String to Enum object.");
	
	/** The next line will generate an error. */
	//OrderStatus status2 = OrderStatus.valueOf("closed");
	
	/** using instead the method added in the Enum. */
	OrderStatus status2 = OrderStatus.fromValue("closed");
	System.out.println(status2 + ": " + status2.getStatus());
    }
}

Output

Enum with field and method example

List all values from enum
CREATED: created
ASSIGNED: assigned
INPROGRESS: inprogress
FINISHED: finished
DELIVERED: delivered
CLOSED: closed

Get one value from enum
DELIVERED
Compare enum with ==
The order has been assigned.

Convert a String to Enum object.
CLOSED: closed