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 type-import-on-demand declaration is a compiler directive that allows all accessible types (classes, interfaces, enums, records, and annotation types) of a named package or a named type to be referenced by their simple names. It utilizes the asterisk (*) wildcard character to instruct the Java compiler to search the specified package or type for unqualified type names encountered in the compilation unit.
// Syntax for importing all types from a named package
import PackageName.*;

// Syntax for importing all member types from a fully qualified named type
import PackageName.TypeName.*;

Technical Characteristics

Compile-Time Resolution This declaration is strictly a compile-time mechanism used for name resolution. It does not dictate runtime class loading behavior or bloat the compiled bytecode. The Java Virtual Machine (JVM) only loads the specific classes that are actively referenced or instantiated during execution. Scope and Shadowing Name resolution in Java follows a strict hierarchy. A single-type-import declaration (e.g., import java.util.Date;) always takes precedence over and shadows a type-import-on-demand declaration (e.g., import java.sql.*;). If a compilation unit contains multiple type-import-on-demand declarations that contain types with identical simple names, no error occurs unless the code actually attempts to use that ambiguous simple name without package qualification. Non-Recursive Evaluation The wildcard (*) applies exclusively to the immediate types within the declared package or the immediate member types within the declared class or interface. It does not recursively traverse the package hierarchy. For example, import java.util.*; makes java.util.List available by its simple name, but it does not import java.util.concurrent.Callable. A separate import java.util.concurrent.*; declaration is required for the subpackage. Member Type Import When the declaration targets a specific type rather than a package (e.g., import com.example.OuterClass.*;), it imports all accessible nested types (static and non-static member classes, and member interfaces) declared within com.example.OuterClass. In Java, all nested interfaces are implicitly static. Import declarations require the canonical (fully qualified) name of the type. Using a simple name like OuterClass will cause the compiler to look for a package named OuterClass and fail, as types in the unnamed package cannot be imported. Furthermore, a type-import-on-demand declaration does not import static fields or static methods; doing so requires a static-import-on-demand declaration (e.g., import static com.example.OuterClass.*;). Implicit Declarations By default, the Java compiler implicitly injects a type-import-on-demand declaration for the core language package into every compilation unit:
import java.lang.*;
Additionally, types within the same unnamed or named package as the current compilation unit are implicitly available on demand without requiring an explicit import statement.
Master Java with Deep Grasping Methodology!Learn More