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.

An import declaration in Kotlin is a file-level directive used to bring declarations from other packages or classifiers into the current namespace, allowing them to be referenced by their simple names rather than their fully qualified names (FQN). Import directives must be declared after the 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.
package org.example.app

import org.example.network.HttpClient

class Service {
    val client = HttpClient() 
}
2. On-Demand (Wildcard) Import Brings all accessible declarations from a specified package or classifier into the current scope. The compiler resolves the specific declarations at compile time.
import java.io.*

fun read(file: File) { 
    // ...
}
3. Aliased Import Introduces a local alias for an imported declaration. This is strictly a file-level rename and is primarily used to resolve namespace collisions between declarations with identical simple names from different packages.
import java.util.Date as UtilDate
import java.sql.Date as SqlDate

val utilDate = UtilDate()
val sqlDate = SqlDate(System.currentTimeMillis())

Importable Entities

Unlike Java, which requires a distinct import 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 object or companion object declarations.
  • Enum constants.
  • Java static methods and fields.
// Importing a top-level function
import kotlin.math.max

// Importing an enum constant directly
import kotlin.annotation.AnnotationTarget.CLASS

// Importing a companion object member
import kotlin.Int.MAX_VALUE

// Importing a Java static method
import java.time.LocalDate.now

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.*
Additional default imports are applied based on the target platform. For the JVM, the compiler implicitly imports:
  • 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.
Master Kotlin with Deep Grasping Methodology!Learn More