![]() | |
|
Static Methods
Since Java 8 in addition to adding default
methods to an interface, you are also able to add
static
methods. The syntax is similar to regular class static
methods: you just use
the static
modifier in the method signature and include a method body:
interface Doable { static void doIt() { System.out.println("Doing it..."); } }
![]() | |
Quote from JLS 8: "An interface can declare This code fails to compile: public class Task implements Doable { public static void main(String[] args) { Doable d = new Task(); d.doIt(); // FAILS !!! } }
This code fails to compile too: public class Task implements Doable { public static void main(String[] args) { Task t = new Task(); t.doIt(); // FAILS !!! } }
This code fails to compile also: public class Task implements Doable { public static void main(String[] args) { Task.doIt(); // FAILS !!! } }
Only this code compiles and runs (the public class Task implements Doable { public static void main(String[] args) { Doable.doIt(); // Prints "Doing it..." } }
|
![]() ![]() ![]() |