The Enum type

Handling constant and fixed value.

Faizal Musa
Jun 4, 2018
Assorted beans

The Java developers are known for grouping related constant values into one handy class file. Typically these classes contain static fields and non-instantiable. These classes are easy to write and use. In the future, the developers could add additional constant values as part of the next release.

To illustrate this, the DepositType class below groups the deposit type names:-

At a glance, this class serves its purpose. All of the constant values are static final. The client uses the values by prefixing the DepositType class name in the code. For example:-

Up to this point, the usage is simple and concise. Then 2 week later, there are additional account type names, codes and blockcodes needs to be supported. The class grows to support additional account codes and blockcodes:-

There is another subtle problem with the static final constant class above. To make it a static constant utility class, fields are accessed as class variables. However, developers usually forget that there is a default constructor if none is supplied.

There is another subtle problem with the static final constant class above. To make it a static constant utility class, fields are accessed as class variables. However, developers usually forget that there is a default constructor if none is supplied.

This problem could be handled easily by using the enum type.

Welcome to the Enum type. Yes, it is quite old and introduced during Sun JDK 5 release(I am feeling old now). Below is an example of an enum type that equivalent to the AccountType class as above.

--

--