ServiceLoader load(Class) in Java

load(Class): This method is available in java.util.ServiceLoader class of Java.

Syntax:

<K> ServiceLoader<K> java.util.ServiceLoader.load(Class<K> service)

This method takes one argument. This method creates a new service loader for the given service type, using the current thread's context class loader.

Parameters: One parameter is required for this method.

service: The interface or abstract class representing the service.

Returns: A new service loader.

Throws:

ServiceConfigurationError - if the service type is not accessible to the caller or the caller is in an explicit module and its module descriptor does not declare that it uses service.

Approach

Java

import java.util.ServiceLoader;

public class ServiceLoaderload {
    public static void main(String[] args) {

        System.out.println(ServiceLoader.load(String.class));
    }

}

Output:

java.util.ServiceLoader[java.lang.String]


No comments:

Post a Comment