ServiceLoader load(ModuleLayer, Class) in Java

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

Syntax:

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

This method takes two arguments. This method creates a new service loader for the given service type to load service providers from modules in the given module layer and its ancestors.

Parameters: Two parameters are required for this method.

layer: The module layer.

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 ServiceLoaderload3 {
    public static void main(String[] args) {

        ModuleLayer moduleLayer = ModuleLayer.boot();
        System.out.println(ServiceLoader.load(moduleLayer,
String.class));
    }

}

Output:

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


No comments:

Post a Comment