JAVA接口中不可以有静态方法吗

如题所述

在jdk1.8中,接口里可以有静态方法,接口里的静态方法 必须要有body。
静态方法不需要实现。

public interface testInter {
void printme();

static void print_s(){
System.out.println("print in static method in interface");
}

}

class testInterImpl implements testInter{

public void printme() {
System.out.println("me");
}
}
public class TestMain {

public static void main(String[] args) {
System.out.println("123");

testInterImpl t = new testInterImpl();
t.printme();
testInter.print_s();
}
}
亲测,1.8可以。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-11-16
接口中每一个方法也是隐式抽象的,接口中的方法会被隐式的指定为 public abstract(只能是 public abstract,其他修饰符都会报错),所以不能含有静态代码块以及静态方法(用 static 修饰的方法)
相似回答