Skip to main content

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.

A static-import-on-demand declaration instructs the Java compiler to resolve unqualified references to all accessible static members—including fields, methods, and nested types—declared within a specified class or interface. By using the wildcard character (*), it brings the entire static namespace of the target type into the current compilation unit’s scope.

Syntax

import static PackageName.TypeName.*;
  • PackageName.TypeName: The fully qualified name of the class or interface containing the static members.
  • .*: The wildcard operator denoting the on-demand inclusion of all static members.

Resolution Mechanics

When the Java compiler encounters an unqualified identifier (e.g., a method call sqrt() or a field access PI), it attempts to resolve the reference in a specific order. Static-import-on-demand declarations are evaluated during this process under the following rules:
  1. Local Scope Precedence: Members declared within the current class or its superclasses always shadow members imported via static-import-on-demand.
  2. Single-Import Precedence: A single-static-import declaration (import static java.lang.Math.PI;) takes precedence over a static-import-on-demand declaration.
  3. Ambiguity Errors: If two different static-import-on-demand declarations import members with identical names and signatures, and the code references that member without qualification, the compiler throws an ambiguous reference error. The conflict must be resolved by either fully qualifying the reference or using a single-static-import for the specific member.

Code Example

// Static-import-on-demand for java.lang.Math
import static java.lang.Math.*;

public class GeometryCalculator {
    
    public double calculateCircleArea(double radius) {
        // 'PI' is resolved to java.lang.Math.PI
        // 'pow' is resolved to java.lang.Math.pow
        return PI * pow(radius, 2);
    }
    
    public double calculateHypotenuse(double a, double b) {
        // 'sqrt' is resolved to java.lang.Math.sqrt
        // 'hypot' is resolved to java.lang.Math.hypot
        return sqrt(pow(a, 2) + pow(b, 2)); 
        // Alternatively: return hypot(a, b);
    }
}

Technical Constraints

  • Accessibility: The declaration only imports members that are accessible to the importing class. private static members of the target class are not imported. Package-private static members are only imported if the importing class resides in the same package.
  • No Type Import: A static-import-on-demand declaration does not import the class type itself. In the example above, referencing the class Math directly (e.g., Math m;) would still require a standard type import (import java.lang.Math;), though java.lang is implicitly imported by default.
  • Inherited Members: The wildcard imports not only the static members declared directly in the specified type but also the accessible static members inherited from its superclasses or superinterfaces.
Master Java with Deep Grasping Methodology!Learn More