Static Methods
Starting with Java 8, it's possible for an interface to provide an implemented static method.
public interface Fillable {
double LITERS_PER_GALLON = 3.78541; // public static final field
void fill(int amount);
static double litersFromGallons(double gallons) {
return gallons * LITERS_PER_GALLON;
}
}
-
A
staticinterface method must have a method body; in an interface a method can't be bothabstractandstatic. -
Remember that
staticmethods can only accessstaticfields; all interface fields arestatic(andpublicandfinal.)
Since they are not abstract, static methods don't change the interface's contract.