8

As far as I know you cannot declare static methods in interface body. However, accidentally I found peculiar piece of code on http://docs.oracle.com/ site. Here is the link

Namelly

public interface TimeClient 
{
void setTime(int hour, int minute, int second);
void setDate(int day, int month, int year);
void setDateAndTime(int day, int month, int year,
                           int hour, int minute, int second);
LocalDateTime getLocalDateTime();

static ZoneId getZoneId (String zoneString) {
    try {
        return ZoneId.of(zoneString);
    } catch (DateTimeException e) {
        System.err.println("Invalid time zone: " + zoneString +
            "; using default time zone instead.");
        return ZoneId.systemDefault();
    }
}

default ZonedDateTime getZonedDateTime(String zoneString) {
    return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
    }
}

this interface has a static method getZoneId

I am lost... could anyone explain please

Benjamin
  • 2,257
  • 1
  • 15
  • 24
Russell'sTeapot
  • 373
  • 2
  • 11
  • 21
  • 4
    You _can_ declare static methods in interfaces starting with Java 8. Note also the `getZoneDateTime()` method (virtual extension method, also new in Java 8). – fge Apr 18 '14 at 06:15
  • 1
    Please read this http://marioosh.5dots.pl/2014/02/12/java-8-default-and-static-methods-in-interfaces.html – Sap Apr 18 '14 at 06:18
  • Haven't you see new feature list of Java 8 ? check it. – Eric Apr 18 '14 at 06:19

5 Answers5

15

You are the witness of two new features in Java 8 here:

  • static methods in interfaces,
  • virtual extension methods.

In the code sample you provide, getZoneId() illustrates the first novelty, and .getZoneDateTime() the second one.

The second one in particular is what has allowed the Collection interface to be extended with supplementary methods such as .stream(), for example, without breaking backwards compatibility. See here for an illustration.

The first one allows to avoid writing "method bags" classes which often exist only to provide utility static methods over interfaces. One such example would be Guava's Functions class (not to be mixed with Java 8's Function which it basically stole from Guava, along with Predicate and a few others)

John Cummings
  • 1,949
  • 3
  • 22
  • 38
fge
  • 119,121
  • 33
  • 254
  • 329
6

Java 8 now has the idea of "default" method implementations in interfaces:

http://blog.hartveld.com/2013/03/jdk-8-13-interface-default-method.html

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • 1
    well everybody answered correctly to my question. You were the first one though. – Russell'sTeapot Apr 18 '14 at 06:33
  • 2
    Not really, there's a difference between default and static methods, so let's not confuse those (and other users). This was maybe a good pointer, but not a correct answer. – virgo47 Apr 12 '16 at 12:22
  • 1
    @virgo47 you're right, I'm not sure what I'm doing here. I looked to see if the question had originally referenced default methods but it didn't. I will delete this. Er, can't delete it as it's accepted :( – Sean Owen Apr 12 '16 at 13:24
5

Starting with Java 8 you can do this. The official tutorial that your snippet came from (which has been updated for Java 8) says it best:

The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation). Default methods are defined with the default modifier, and static methods with the static keyword. All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

Or from Java 8's JLS section 9.4:

A default method is a method that is declared in an interface with the default modifier; its body is always represented by a block. It provides a default implementation for any class that implements the interface without overriding the method. Default methods are distinct from concrete methods, which are declared in classes.

...

An interface can declare static methods, which are invoked without reference to a particular object.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
5

From Java Language Specification (Java SE 8 Edition): http://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4

An interface can declare static methods, which are invoked without reference to a particular object.

Ιναη ßαbαηιη
  • 3,410
  • 3
  • 20
  • 41
4

in Java 8, interfaces can have static methods, as well as override-able methods with a default implementation. They still can't have instance fields though. These features are part of the lambda expression support, and you can read more about them in Part H of. JSR 335

Sanjay Rabari
  • 2,091
  • 1
  • 17
  • 32