Before Java 8, you couldn't define static
methods in an interface
. This is heavily discussed in this question. I'm going to refer to this answer (by user @JamesA.Rosen) as to why the Java designers probably didn't want static
methods in an interface
initially:
There are a few issues at play here. The first is the issue of
declaring a static method without defining it. This is the difference
between
public interface Foo {
public static int bar();
}
and
public interface Foo {
public static int bar() {
...
}
}
Java doesn't allow either, but it could allow the second. The first is
impossible for the reasons that Espo mentions: you don't know which
implementing class is the correct definition.
Java could allow the latter, as long as it treated Interfaces as
first-class Objects. Ruby's Modules, which are approximately
equivalent to Java's Interfaces, allow exactly that:
module Foo
def self.bar
...
end
end
However, since the release of Java 8, you can actually add default
and static
methods inside an interface
.
I'm going to be quoting this source a lot here. This is the initial problem:
Java's interface language feature lets you declare interfaces with
abstract methods and provide implementations of those methods in the
classes that implement the interfaces. You are required to implement
each method, which is burdensome when there are many methods to
implement. Also, after publishing the interface you cannot add new
abstract methods to it without breaking source and binary
compatibility.
This was the solution Java 8 provided default
:
Java 8 addresses these problems by evolving the interface to support
default and static methods. A default method is an instance method
defined in an interface whose method header begins with the default
keyword; it also provides a code body. Every class that implements the
interface inherits the interface's default methods and can override
them
And for static
:
A static method is a method that's associated with the class in which
it's defined, rather than with any object created from that class.
Every instance of the class shares the static methods of the class.
Java 8 also lets static methods be defined in interfaces where they
can assist default methods.
When you implement an interface that contains a static method, the
static method is still part of the interface and not part of the
implementing class. For this reason, you cannot prefix the method with
the class name. Instead, you must prefix the method with the interface
name
Example:
interface X
{
static void foo()
{
System.out.println("foo");
}
}
class Y implements X
{
}
public class Z
{
public static void main(String[] args)
{
X.foo();
// Y.foo(); // won't compile
}
}
Expression Y.foo()
will not compile because foo()
is a static member
of interface X
and not a static member of class Y
.
Default Methods
? – MadeiraCat
can only extend one class so ifCat
extendsAnimal
,Cat.identify
has only one meaning.Cat
can implement multiple interfaces each of which can have a static implementation. Which one would be right? – Clarenceclarenceuxdefault void identify()
, which one is used? It's a compile error, and you have to implement an overriding method (which could just beAnimal.super.identify()
). So Java already resolves this problem for default methods – why not for static methods? – Dramdefault
the implementation is part ofCat
'svtable
. Withstatic
it cannot be. Themain
function must bind to something. At compile timeCat.identify
could be replaced withAnimal.identify
by the compiler but the code wouldn't match reality if Cat was recompiled but not the class that containsmain
. – Clarenceclarenceux