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 interface
into a class
somewhere in future, or move these fields into a class, and you'll get a semantical difference if you overlook some fields defined without public static final
(of course, we have tools for refactoring, but nonetheless).
I think it's the same thing as support of @Overriden
annotation 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.
I don't think so. All interface variables are implicitly public static final so no meaning to mark them same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With