Why we use public static final declaration of instance variables in a Java Interface?
All the variables are implicitly public static final in a Java Interface.
Is it a good coding practice to use public static final in constant variable although it is declared inside an Interface.
For example :
public interface TestInterface{
public static final String EX_CONSTANT = "ABC";
public static final int EX_INT_CONSTANT = 5;
public static final double EX_DOUBLE = 5.0;
public static final Integer EX_INTEGER = 10;
}
Use of uniform syntax in both classes and interfaces simplifies refactoring.
You may want to turn your
interfaceinto aclasssomewhere in future, or move these fields into a class, and you’ll get a semantical difference if you overlook some fields defined withoutpublic static final(of course, we have tools for refactoring, but nonetheless).I think it’s the same thing as support of
@Overridenannotation for implementations of methods declared in interfaces that was introduced in Java 6 – it’s redundant in its current form, but may become useful in case of refactoring.