-2

Java8 allows interface to have static method. It would be really helpful if anybody explain in which scenario we might need to go for interface with static methods.

Thanks in advance.

Rakesh
  • 1,374
  • 1
  • 16
  • 24

3 Answers3

3

Well did you search the jdk sources? How about at least two examples:

 Function.identity()

that has an implementation as :

static <T> Function<T, T> identity() {
    return t -> t;
}

Or Predicate.isEqual that looks like:

static <T> Predicate<T> isEqual(Object targetRef) {
    return (null == targetRef)
            ? Objects::isNull
            : object -> targetRef.equals(object);
} 

Generally I treat them as static factory methods that return an instance of that interface back.

I have a great example of this that we use in our code base (but it comes from Holger initially):

public interface UncheckedCloseable extends Runnable, AutoCloseable {
    @Override
    default void run() {
        try {
           close();
        } catch (Exception ex) {
           throw new RuntimeException(ex);
        }
    }

    static UncheckedCloseable wrap(AutoCloseable c) {
       return c::close;
    }

    default UncheckedCloseable nest(AutoCloseable c) {
        return () -> {
            try (UncheckedCloseable c1 = this) {
                c.close();
            }
        };
    }
 } 
Eugene
  • 117,005
  • 15
  • 201
  • 306
1

Resource can be found here and here:

  1. Java interface static method is part of interface, we can’t use it for implementation class objects.
  2. Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
  3. Java interface static method helps us in providing security by not allowing implementation classes to override them.
  4. We can’t define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”. This is because it’s not allowed in java, since
  5. Object is the base class for all the classes and we can’t have one class level static method and another instance method with same signature.
  6. We can use java interface static methods to remove utility classes such as Collections and move all of it’s static methods to the corresponding interface, that would be easy to find and use.
Beri
  • 11,470
  • 4
  • 35
  • 57
0

From what I have understood-

1.These methods cannot be overridden by classes that inherit it.

2.They are accessible only to the functions of that interface(must not be overridden).

So, they can be used whenever you want that the function(which is not static nor overridden) of your interface to use that function(which is static).

So you can use your own business logic in it, your own sorting methods, some limitations or boundations.

So it can be used as if someone calls your function and you do your own stuff in it with help of other functions. It is like that the other programmer implements your interface to use some function that provides some support to its own program(eg. calendar that can save notes, plan your meetings, etc).

But remember you should not have overridden the function that calls those static function.

sOOsOOry
  • 211
  • 2
  • 4