static 키워드를 이용해도 인터페이스에 메서드를 구현할 수 있습니다. Calculator 인터페이스에 static 메서드를 구현하겠습니다.
package javaStudy;
public interface Calculator {
public int plus(int i, int j);
public int multiple(int i, int j);
default int exec(int i, int j) {
return i + j;
}
public static int exec2(int i, int j) {
return i * j;
}
}
역시 에러가 나지 않습니다. 인터페이스에 정의한 static 메서드를 사용하겠습니다.
package javaStudy;
public class MyCalTest {
public static void main(String[] args) {
Calculator cal = new MyCal();
cal.plus(3, 4);
int i = cal.exec(5, 6);
System.out.println(i);
cal.exec2
}
}
실행결과
exec2 cannot be resolved or is not a field