java:S1214 Constants should no be defined in interfaces triggers for interfaces having methods

The rule description says “According to Joshua Block, the constant interface pattern is a poor use of interfaces.” True, I fully agree.

However, Joshua Block also says that a constant interface is “an interface which contains no methods, it consists solely of static fields, each exporting a constant.”

So, IMHO, this rule should not trigger for interfaces having methods, even if it define a constant.

Problem seen with SonarQube 7.9.2 CE using Java plugin V6.3.2.


Can you provide a reference for this citation?

In any case, I look into the subject and reached the same conclusion as you.
I therefore created a ticket for it: (SONARJAVA-3477)



The citation comes from:
Joshua Bloch: Effective Java, 2nd edition. Addison Wesley, 2008.
p. 98: “Item 19: Use interfaces only to define types”

The item starts with:

When a class implements an interface, the interface serves as a type that can be used to refer to instances of the class. That a class implements an interface should therefore say something about what a client can do with instances of the class. It is inappropriate to define an interface for any other purpose.

One kind of interface that fails this test is the so-called constant interface. Such an interface contains no methods; it consists solely of static final fields, each exporting a constant.

Best regards,

Perfect, thanks for the precision.
And thank you again for raising this point.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.