> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Uses Directive

The `uses` directive is a module declaration statement within the Java Platform Module System (JPMS) that specifies a service interface or abstract class consumed by the current module. It explicitly declares the module's intent to dynamically discover and load implementations of that service at runtime via the `java.util.ServiceLoader` API, establishing a consumer-provider relationship without creating a compile-time dependency on the implementation classes.

## Syntax

The directive is declared exclusively within a `module-info.java` file. It requires the name of the service type, which can be specified as a fully qualified name or as a simple name if the type is imported prior to the `module` keyword (per JLS §7.3).

```java theme={"dark"}
import com.example.serviceapi.MyService;

module com.example.consumer {
    // Requires the module containing the service interface
    requires com.example.serviceapi;
    
    // Declares the intent to consume implementations of the interface using a simple name
    uses MyService;
    
    // Alternatively, using a fully qualified name without an import:
    // uses com.example.serviceapi.AnotherService;
}
```

## Technical Mechanics

**ServiceLoader Coupling**
The `uses` directive is strictly bound to the `ServiceLoader` mechanism. If a module attempts to invoke `ServiceLoader.load(MyService.class)` without explicitly declaring `uses MyService;` in its module descriptor, the `java.util.ServiceLoader` library API will throw a `java.util.ServiceConfigurationError` at runtime. The `uses` directive authorizes the `ServiceLoader` to locate providers on behalf of the consumer module. It does *not* grant the consumer module reflective access to instantiate the provider classes directly; instantiation is handled internally by the `ServiceLoader` (in `java.base`) and is authorized by the `provides` directive within the provider module.

**Module Resolution and Binding**
When launching an application, the JVM constructs its boot layer using `java.lang.module.Configuration.resolveAndBind()`. This means standard module resolution at runtime *automatically* performs service binding. The JPMS will automatically pull observable provider modules from the module path into the module graph if they provide an implementation for a service declared in a resolved module's `uses` directive.

Conversely, when assembling a custom runtime image using the `jlink` tool, service binding is *not* performed by default. To include provider modules in a generated `jlink` image based on `uses` directives, the `--bind-services` command-line flag must be explicitly passed to the `jlink` tool.

**Type Constraints**
Per JLS §7.7.4, the type specified in the `uses` directive must specifically be a class or interface type. While arrays are reference types in Java, they are syntactically invalid in a `uses` directive. The consuming module must have access to this type. If the type is defined in an external module, the consuming module must declare a `requires` directive for that external module.

## Compilation vs. Runtime Behavior

* **Compile-Time:** The Java compiler (`javac`) verifies that the class or interface type specified in the `uses` directive exists and is accessible to the declaring module. It does *not* require an implementation of the service to be present on the module path during compilation.
* **Runtime:** The module system utilizes the resolved module graph, augmented by automatic service binding, to locate provider classes. The consumer module remains entirely decoupled from the provider modules, interacting with them solely through the service type's API.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
