Interface with constants only

I recently came across a piece of code in which I found an interface with constants only . And these constants were available in classes using static imports. The constants were larger (from 30 to 50).

Personally, I do not consider this a good practice. That's why it is called the Constant Interface Antipattern in accordance with efficient Java. I find no reason for such coding.

In addition, static import should be used ONLY if in our application several classes are imported by several classes.

Can any of you please tell me if there are any other good reasons to use for interface constants only?

+3
source share
8

, , , , , Constant Interface.

, ( ", " ) - , , .

Java , (). , , , .

+5

, enum

+3

. , ? .

, , , . .

enum - . .

+2

, . , , " " , , , .

- .

+1

, , , , .

, .

0

. , , . , , .

0

Another alternative (besides enum) would be a non-stationary class (private constructor) for collecting constants if the enumeration did not fit (for example, strings, integers, etc.).

In addition, it is possible that the class will be visible to the package only where it is used.

0
source

All Articles