Java

Java Enum example with advantages and disadvantages


Enum feature is originally inherent from C and C++ language. It is also known as enumeration.

What is Enum?

An enum type is a special data type that enables for a variable to be a set of predefined values. The variable must be equal to one of the values that have been predefined for it.

For example, you give a rating to your employee performance, Number of planets in our Solar system, Number of months etc. It is introduced in JDK 1.5 version.

What is behind the Java Enum?

It is improve readability without writing a lot of boilerplate. If you are willing to write boilerplate, then enums look like this:

public class Rating {
	
	private final int ratingValue;
	
	public static final Rating EXCEEDS_EXPECTATIONS = new Rating(6);
	public static final Rating EXCELLENT = new Rating(5);
	public static final Rating SUPERB = new Rating(4);
	public static final Rating NICE = new Rating(3);
	public static final Rating NEEDS_IMPROVEMENT = new Rating(2);
	public static final Rating POOR = new Rating(1);
	
	public Rating(int ratingValue){
		this.ratingValue = ratingValue;
	}
	
	public int getRating(){
		return ratingValue;
	}
}

The Java enum constants are static and final essentially. It’s likewise in Enum:

public enum RatingEnum {
	EXCEEDS_EXPECTATIONS(6), EXCELLENT(5), SUPERB(4), NICE(3), NEEDS_IMPROVEMENT(2), POOR(1);

	private int value;

	public int getRating() {
		return value;
	}

	private RatingEnum(int value) {
	  this.value = value;
	 }
}
public class EnumTest {
	public static void main(String[] args) {
		System.out.println("Rating is (Using Class)::"+RatingEnum.EXCEEDS_EXPECTATIONS.getRating());
		System.out.println("Rating is (Using Enum)::"+Rating.EXCEEDS_EXPECTATIONS.getRating());
	}
}

Java enum with switch case statement

public class EnumSwitchCaseTest {
	public static void main(String[] args) {
		
		RatingEnum myRating = RatingEnum.valueOf(RatingEnum.class, "EXCEEDS_EXPECTATIONS");
		
		switch (myRating) {
		case EXCEEDS_EXPECTATIONS:
			System.out.println("Rating Value ::"+RatingEnum.EXCEEDS_EXPECTATIONS.getRating());
			break;
		case EXCELLENT:
			System.out.println("Rating Value ::"+RatingEnum.EXCELLENT.getRating());
			break;
		case SUPERB:
			System.out.println("Rating Value ::"+RatingEnum.SUPERB.getRating());
			break;
		case NICE:
			System.out.println("Rating Value ::"+RatingEnum.NICE.getRating());
			break;
		case NEEDS_IMPROVEMENT:
			System.out.println("Rating Value ::"+RatingEnum.NEEDS_IMPROVEMENT.getRating());
			break;
		case POOR:
			System.out.println("Rating Value ::"+RatingEnum.POOR.getRating());
			break;
		default:
			break;
		}
	}
}

How to Iterate Java Enumeration

Using Java 1.5+

for(RatingEnum rating: RatingEnum.values()){
			System.out.println(rating);
		}

Using Java 8

Stream.of(RatingEnum.values()).forEach(System.out::println);

How to access enum values in JSP using JSTL


<c:set var="myRating">
<%=RatingEnum.EXCEEDS_EXPECTATIONS.getRating()%>
</c:set>

<c:if test="${6 == myRating}">
....
</c:if>

Why use Enum? (advantage of enum)

  • Enumeration being used as a singleton pattern so it gives thread safety benefits.
  • When you deal with serialization enum is suitable options because it gives you guaranteed to be unique just check them using == operator.
  • Being lazy programmer enum avoids a lot of boilerplate code.
  • It is easy to use with java collections.
  • It allow us to store additional value with each enum constant.
  • For batter readability.
  • Enum inherits all the methods of Object class and abstract class Enum. So you can use its methods for comparable, reflection, serialization etc. If you just declare a static constant instead of Enum, you can’t do this.
  • Enums are iterable like collections. (Using values() method)
  • Ability to use values in switch case statements without restriction.

Why not use Enum? (disadvantages of enum)

  • It is much like class it’s extend java.lang.Enum so you can’t extend other enum.
  • Another potential problem is you don’t get along with Expression Language (EL) in JSP.
  • There are things that can be done in normal classes but maybe you can not do with enum class because of it is a special class. For example, accessing a static field in the constructor that not possible with enum.
  • When you working with JSP then you can not be accessing enums nor calling enum constants because it’s not supported (which is possible after EL version 3.0).

Reference

  1. Java Documentation : Enum Types
  2. Advantages of Java’s enum
  3. Enum vs public static final fields
  4. What are enums and why are they useful?
Java
Java Copy File with 5 different ways
Java
4 ways to Convert String to int in Java
There are currently no comments.