Java 8 code KATAs II – Interface with static methods Example

In this series of posts, I will be posting simple java 8 code katas.

Part I – Interface with default methods
Part II – Interface with static methods
Part III – Functional Interfaces & Lambda Expressions
Part IV – Method References
Part V – java.util.function package
Part VI – Streams

This post would be about interface with static methods.

Java 8 allows interface to have static methods with implementations.

See the example below:

InterfaceWithStaticMethodsMain.java


package com.idodevjobs;

public class InterfaceWithStaticMethodsMain {

    public static void main (String[] args) {
        System.out.println(InterfaceWithStaticMethods.sayHello("test"));
    }
}

interface InterfaceWithStaticMethods {
    static String sayHello(String message) {
        return "hello " + message;
    }
}


In this example sayHello is a static method with implementation in InterfaceWithStaticMethods interface. The main class InterfaceWithStaticMethodsMain uses the sayHello method in InterfaceWithStaticMethods interface without any issue. An interface can contain any number of static methods.

Advertisements
This entry was posted in java and tagged . Bookmark the permalink.

5 Responses to Java 8 code KATAs II – Interface with static methods Example

  1. Pingback: Java 8 code KATAs I – Interface with default methods Example | iDoDevJobs

  2. Pingback: Java 8 code KATAs IV – Method References Example | iDoDevJobs

  3. Pingback: Java 8 code KATAs III – FunctionalInterface & Lambda Expressions Example | iDoDevJobs

  4. Pingback: Java 8 code KATAs V – java.util.function Example | iDoDevJobs

  5. Pingback: Java 8 code KATAs VI – Streams | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s