A static-import-on-demand declaration instructs the Java compiler to resolve unqualified references to all accessibleDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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
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 callsqrt() 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:
- Local Scope Precedence: Members declared within the current class or its superclasses always shadow members imported via static-import-on-demand.
- Single-Import Precedence: A single-static-import declaration (
import static java.lang.Math.PI;) takes precedence over a static-import-on-demand declaration. - 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
Technical Constraints
- Accessibility: The declaration only imports members that are accessible to the importing class.
privatestatic 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
Mathdirectly (e.g.,Math m;) would still require a standard type import (import java.lang.Math;), thoughjava.langis 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





