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

Python010

JAVA接口中不可以有静态方法吗,第1张

在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可以。

接口的所有方法都是抽象的,而抽象方法是没有static,有static的方法是不能override的,所以这样定义接口才有意义。

接口中的变量也都是Final、static。