Java – should a class implement an interface with only constants?

Today, I looked at the zipentry class and found the following:

public class ZipEntry implements ZipConstants,Cloneable

Zipconstants does not define any method – only constants (static final int lochdr = 30)

Then I found that implementing the interface with constants allows you to access these constants directly as if they were defined in the class itself For example:

public interface Constants {
    static final int CONST = 2;
}

public class implements Constants {
    int doSomething(int input) {
        return CONST * input;
    }
}

Are there any other reasons not to use this except:

>This constant comes from the initial confusion > using interfaces to define constants is considered wrong

I'm curious because it's definitely not a common practice

Solution

Another reason not to use this:

Starting with Java 5, there is a "clean" language function that can achieve the same goal: static imports

The interface that implements constants is basically hack before java-5 that simulates static import

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>