package directive (if one exists) and before any top-level declarations (classes, functions, properties, or objects).
Syntax and Types of Imports
Kotlin supports three primary forms of import declarations: 1. Single-Name Import Brings a specific, single declaration into the current file scope.Importable Entities
Unlike Java, which requires a distinctimport static directive for static members, Kotlin uses a unified import syntax for all entities. You can import:
- Top-level classes and interfaces.
- Top-level functions and properties.
- Functions and properties declared inside
objectorcompanion objectdeclarations. - Enum constants.
- Java static methods and fields.
Default Imports
The Kotlin compiler implicitly injects a standard set of import directives into every Kotlin file. This eliminates the need to manually import core language constructs. The universal default imports include:kotlin.*kotlin.annotation.*kotlin.collections.*kotlin.comparisons.*kotlin.io.*kotlin.ranges.*kotlin.sequences.*kotlin.text.*
java.lang.*kotlin.jvm.*
Scope and Resolution Mechanics
- File-Scoped: The scope of an import declaration is strictly limited to the file in which it is defined. It does not leak into other files within the same package.
- Visibility Agnostic: Import declarations do not bypass visibility modifiers (
private,internal, etc.). You can only import declarations that are visible to the current file based on Kotlin’s standard visibility rules. - Resolution Priority: During scope resolution, explicit (single-name) imports have a higher priority than declarations defined in the current package. Conversely, declarations in the current package take precedence over entities brought in via on-demand (star) imports. If a naming collision occurs between an explicit import and a declaration in the current package, the explicit import shadows the package declaration, and a fully qualified name must be used to access the package-level entity.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More





