Interface Variables

2019-08-16 19:59发布

问题:

public interface A 
{

    public final int a = 0;

}

Many books say that all variables (constants) in an interface are implicitly public static final yet when I type the above statement explicitly but do not include the keyword static it compiles with no errors and can be referenced by the static way, A.a which indicates that it is still static.

Is it static or not, as to me it has to be since you cannot instantiate an interface, as if you had this "instance" variable then you could therefore never access the data member "a" as it is a non-static field.

回答1:

Many books say that all variables (constants) in an interface are implicitly public static final

Yes, and what you're observing is exactly consistent with that. The key word is implicitly: it is a static variable whether or not you write the word static.