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-type-import declaration makes a specific type (class, interface, enum, annotation, or record) from another package available by its simple name within the current compilation unit. It establishes a direct mapping between the fully qualified name (FQN) of the type and its simple name, instructing the compiler to resolve the simple name to the FQN during the compilation phase.
import PackageName.SimpleTypeName;
Structural Placement The declaration must appear after the package declaration (if one exists) and before any top-level type declarations within the .java source file. Resolution and Accessibility The compiler strictly evaluates the FQN provided in the declaration. If the specified type does not exist, or if it lacks the appropriate access modifiers (e.g., attempting to import a package-private type from a different package), the compiler throws an error. Naming Conflicts and Shadowing
  • Import Collisions: It is a compile-time error to declare multiple single-type-imports that resolve to the same simple name but different FQNs.
import java.util.Date;
import java.sql.Date; // Compile-time error: Date is already defined in a single-type import
  • Precedence over Import-on-Demand: A single-type-import declaration shadows any type with the identical simple name that is imported via an import-on-demand declaration (e.g., import java.util.*;).
  • Conflict with Local Types: It is a compile-time error if a single-type-import declaration has the same simple name as a top-level type declared within the same compilation unit. The local type does not shadow the import; instead, the compiler rejects the duplicate name definition.
Redundancy A single-type-import declaration is considered redundant, though syntactically legal, if it targets:
  1. A type within the java.lang package, which the compiler imports implicitly.
  2. A type within the same package as the current compilation unit.
  3. A type that has already been declared in a previous single-type-import within the same file.
Nested Types A single-type-import can be used to import a nested type by referencing its enclosing class in the FQN. However, importing an enclosing class does not implicitly import its nested types; they must be referenced using their qualified names relative to the imported enclosing class, or imported explicitly via their own single-type-import declarations.
// Importing a nested type directly
import java.util.Map.Entry; 
Master Java with Deep Grasping Methodology!Learn More