I know that in Java, someone can include constants inside interfaces by declaring them public static final. However, suppose I have such an interface, called I, which contains only public static final constants and no method signatures, and a class called C. Why would I want to implement I in C? i.e. why would I do the following:
public class C implements I
Since the constants are public and static, wouldn’t I be able to access them directly through I?
This (anti-)pattern is useful because it lets you use the names of those constants without having to prefix them with
I.. This used to be a common technique, but now that you can useimport staticto import the constants the same way it’s fallen out of favor. One of the reasons for avoiding it is that the set of constants really isn’t an interface – it’s just a bunch of values – and making it an interface lets you do bizarre things like writingor
which just don’t make sense in this context.
Hope this helps!