I know what you're saying.
I for example, want to have an interface (which I am calling ExecutableList) have a contant String field called USER_FRIENDLY_NAME
public interface ExecutableList<T extends ExecutableList<T,E>,E> //This is type parameter
extends List<E>, //self referencing. Trust
Comparable<E>, //me, it has its uses.
{
/** This would declare a constant but leave it to sub-interfaces/classes to define it. */
abstract String USER_FRIENDLY_NAME;
//Define the rest of your interface
}
Unfortunately, this is not possible in Java. When you declare a field in a Java interface, it implies the modifiers public
, static
, and final
. A future change to the Java Programming Language MIGHT allow for abstract
to be added, thus making the implied modifiers public abstract static final String USER_FRIENDLY_NAME;
but while you could clearly define rules for this, the use of the word abstract
would be confusing, as abstract
and final
are typically understood to define opposite meanings.
I'm crossing my fingers that a future version of Java will implement this. I have heard that private interface methods will be added in Java 9 (for use inbetween default methods) but no word yet on this addition to the language. There'd be a number of backwards compatability issues to consider first, I'm sure. If anyone from Oracle reads this, PLEASE LET US DECLARE CONSTANTS IN INTERFACES AND LET INHERITING CLASSES/INTERFACES DEFINE THEM!!!
In the meantime, your only option is to define a String getUserFriendlyName();
method in your interface or abstract class. Not as elegant and perhaps not as effecient, but you have no other option currently.