Use a Java Enum with Strings

Posted by in Software

In my project I wanted to log messages using predefined categories. Traditionally, I do it via classes (if the messages are specific to the functions provided by that class):

or an interface (pretty much the same thing):

These solutions are prone to errors (e.g. when performing copy-paste and forgetting to change the string), so I wondered if there is an enum-based approach.

Of course there is:

Quite nice, but more verbose than the interface variant (and also prone to the copy-paste duplication). That’s why I ended up sticking with the interface approach for the time being.


A little experiment: If you find this post and ad below useful, please check the ad out :-)