Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public static final declaration of an instance variables in JAVA Interfaces

Tags:

java

interface

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;

}
like image 276
Shreyos Adikari Avatar asked Sep 20 '12 18:09

Shreyos Adikari


2 Answers

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.

like image 154
axtavt Avatar answered Oct 14 '22 07:10

axtavt


I don't think so. All interface variables are implicitly public static final so no meaning to mark them same.

like image 45
Subhrajyoti Majumder Avatar answered Oct 14 '22 06:10

Subhrajyoti Majumder