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 single-static-import declaration imports a specific static member (field, method, or nested type) from a named type into the current compilation unit. This allows the compiler to resolve the imported static member by its simple name rather than requiring its fully qualified type name.

Syntax

import static PackageName.TypeName.Identifier;
  • PackageName.TypeName: The fully qualified name of the class or interface containing the static member.
  • Identifier: The exact simple name of the static field, static method, or static nested type being imported.

Technical Mechanics

  • Method Overloading: If the Identifier refers to a static method, the declaration imports all overloaded static methods bearing that exact name within the target type. You cannot import a specific method signature; you import the method name.
  • Accessibility: The targeted static member must be accessible to the importing compilation unit. If the member is private, or package-private and residing in a different package, a compile-time error occurs.
  • Scope and Shadowing: The imported static member is introduced into the scope of the compilation unit. However, members declared within the current class (as well as inherited members) take precedence and will shadow the statically imported member.
  • Naming Conflicts: If two single-static-import declarations attempt to import fields or nested types with the identical simple name from different types, a compile-time error occurs immediately at the import declaration level. However, Java allows statically importing methods with the same simple name from different types. These imported methods simply overload each other. A compile-time error only occurs for methods if the conflicting imports result in methods with the exact same signature (JLS §7.5.3).
  • Placement: The declaration must appear after the package declaration (if present) and before any top-level type declarations (class, interface, enum, record).

Code Visualization

package com.example.core;

// Single-static-import of a static field
import static java.lang.Math.PI;

// Single-static-import of a static method (imports all overloads of 'sort')
import static java.util.Collections.sort;

// Valid: Overloads 'sort' with methods from another type, provided signatures are completely disjoint
import static java.util.Arrays.sort;

import java.util.List;

public class Computation {
    public void process(List<String> list, int[] array) {
        // The compiler resolves 'sort' to java.util.Collections.sort(List)
        sort(list);
        
        // The compiler resolves 'sort' to java.util.Arrays.sort(int[])
        sort(array);
        
        // The compiler resolves 'PI' to java.lang.Math.PI
        double circleConstant = PI; 
    }
}
Master Java with Deep Grasping Methodology!Learn More