Java 8 default and static methods example

In this tutorial I am going to show how default and static methods can be included into Java 8 interface. Prior to Java 8 we did not have such mechanism to include default method or static method into interface but Java 8 provides such option to include those methods. Static methods are regarded as utility methods, not as essential, core methods. Whereas default methods are treated as common behaviors for all the client classes.

When you extend an interface that contains a default method, you would get the following benefits:

  • If you do not mention the default method at all, which lets your extended interface inherit the default method.
  • If you re-declare the default method, which makes it abstract.
  • If you redefine the default method, which overrides it.

You can fine more info here
Example on default and static methods in interface

package intrface;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public interface InterfaceTest {
    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();
    static String getMsg() {
        return "Hello, World!";
    default ZonedDateTime getZonedDateTime(String zoneString) {
        return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
    default String getHello() {
        return "Hello";

Client class

package intrface;
import java.time.LocalDateTime;
public class InterfaceTestClient implements InterfaceTest {
    public LocalDateTime getLocalDateTime() {


package intrface;
public class TestInterfaceTestClient {
    public static void main(String[] args) {
        InterfaceTest interfaceTest = new InterfaceTestClient();
        System.out.println("Current Time: " + interfaceTest.getLocalDateTime());
        System.out.println("Zonal Time: " + interfaceTest.getZonedDateTime("Asia/Kolkata"));
        System.out.println("getHello: " + interfaceTest.getHello());
        System.out.println("getZoneId: " + InterfaceTest.getZoneId("Asia/Kolkata"));
        System.out.println("getMsg: " + InterfaceTest.getMsg());


Current Time: 2017-05-26T08:41:15.009
Zonal Time: 2017-05-26T08:41:15.011+05:30[Asia/Kolkata]
getHello: Hello
getZoneId: Asia/Kolkata
getMsg: Hello, World!

Thanks for reading.

Leave a Comment