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

3
  • 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). Commented Apr 18, 2014 at 6:15
  • 1
    Please read this marioosh.5dots.pl/2014/02/12/… Commented Apr 18, 2014 at 6:18
  • Haven't you see new feature list of Java 8 ? check it. Commented Apr 18, 2014 at 6:19

5 Answers 5

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)

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

well everybody answered correctly to my question. You were the first one though.
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 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 :(
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.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.