Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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)

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top