I'm being forced to use the common anti-pattern of a public interface Constants
which many classes in this project implement. Long story short, I need to have a List
constant which is pre-populated with values. Normally I would do this like so:
public static final List<String> MY_CONSTANT = new ArrayList<String>();
static {
MY_CONSTANT.add("foo");
MY_CONSTANT.add("bar");
// ...
}
Unfortunately, I can't use static initializers in an interface. So after much frustration, I finally have my implementation as follows:
public static final List<String> MY_CONSTANT = new ArrayList<String>() {
private static final long serialVersionUID = 1898990046107150596L;
{
add("foo");
add("bar");
// ...
}
}
I hate the fact that I've made an anonymous extension like this and it makes me cringe. Are there any better techniques I can use to accomplish this? Changing the Constants
file to a class
instead of an interface
isn't an option. As I said, I'm just retouching a large, pre-existing code base.