Java Enums

Java Enums for security, maintainability and reduced code complexity

Java Enums
Image Source - http://hubpages.com/@alexk2009
Java enums let you specify global constants in one place, mitigate security risks and

Enums let Java developers define constants and create singletons and avoid defining PUBLIC STATIC FINAL objects.

Enums can also hold data and associate different behaviour to each Enum constant. This can reduce code and maintenance dramatically. Use of Enum constants can also help mitigate some security issues.

 

The simple enum

 

public enum SimpleEnum {ONE,TWO;}

 

This provides two immutable constants. Once upon a time these would have been PUBLIC STATIC FINAL objects e.g

public static final Integer ONE = “One”;

This let an IDE such as Eclipse warn of typing mistakes and human memory bugs but resulted in classes with vast numbers of strings that had, for safety, to be unique throughout the application. The situation was even worse with Integers as some developers would remember the value of the constant and use the value not the constant. This resulted in bugs other developers loved to spend hours tracking down: after which they might track down the perpetrator and inform them forcefully of the error of their ways.

Enums allow packaging constants needed by a class or system in one place in compact easily maintainable fashion.

No more worrying about capitalisation (I recall a typo in a manual that cost three people a week of debugging till someone suggested changing the initial latter of a string from lowercase to uppercase, in contradiction of the manual).

Enums with data

 

Enum constants can also hold data for example

 

  1. public enum ComplexEnum {

  2. UK("hello", "world"), US("hi", "man");

  3. private final String first,second;

  4. //Mandatory constructor. The order of arguments

  5. //in the constructor matches the order in the constant.

  6. private ComplexEnum(String w1, String w2) {

  7.             first = w1;

  8.            second = w2;

  9. }

  10. public String eval(){ return first + " " + second;}

  11. public String getFirst(){ return first; }

  12. public String getSecond(){return second;}

  13. }

 

 

  1. The Strings need not be final, but if they are not final accessor methods should be defined.
  2. The methods apply to all constants: UK.eval() returns “Hello world” and US.eval() returns “Hi Man”

One can also create different behaviours for each constant by defining an abstract function eval on the class and implementing it for each constant.

  1. public enum Operation {

  2.   PLUS   { double eval(double x, double y) { return x + y; } },

  3.   MINUS  { double eval(double x, double y) { return x - y; } },

  4.   TIMES  { double eval(double x, double y) { return x * y; } },

  5.   DIVIDE { double eval(double x, double y) { return x / y; } };

  6.   // Do arithmetic op represented by this constant

  7.   abstract double eval(double x, double y);

  8. }

( source: http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.html )

Enums can be confusing at first but eliminate methods like

 

public getStringfromType(ComplexEnum type){

if(type==UK)return “hello”

if()

…..

}

 

Similarly creating an eval() method for each constant can radically simplify the design of an application.

Since an Enum constant is immutable it can safely be included in SQL calls without creating an SQL injection vulnerability.

 

The Wrap

  • Enums provide typesafe constants and allow data and methods to be associated with each constant.
  • One can think of an Enum as a way to encapsulate a base class and a set of sub classes (Enum constants) each with different implementations of a method and different field values, accessed through protected accessors.
  • Used intelligently enums can reduce the size and maintenance load of code, avoid some types of security vulnerability and dramatically reduce the size and complexity of an implementation.

 

You can see more of my technological musings on http://digitalsteampunk.blogspot.co.uk/


Let us know how you like this article. Like it and Rate it below.
128 0
0
0 stars - by 0 user(s)

Related Articles

Post Your Comment

There are no comments yet.