Search code examples
javaenumsstringconstants

Java: `enum` vs `String` as Parameters


I've been reading through the details of the System libraries set and get methods yet the parameters are usually Strings.

Would you consider the use of String as parameters bad practise since the inclusion of enum?

A better alternative at minimum might be public final String, No?


Solution

  • I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings.

    As a pre Java 1.5 alternative you could use the type-safe enum pattern suggested by Joshua Bloch in his book Effective Java. For type-safe enums see also http://www.javacamp.org/designPattern/enum.html