In a Kotlin project, code is organized using packages and imports:
- A package is a container for one or more Kotlin files. Files are linked to a package using a
packageheader. - An import is a directive that makes entities from other packages available in the current file.
A source file may start with a package declaration:
package org.example
fun printMessage() { /*...*/ }
class Message(val text: String) { /*...*/ }All contents of the source file (such as classes and functions) belong to this package. In the example above:
- The fully qualified name of
printMessage()isorg.example.printMessage. - The fully qualified name of
Messageisorg.example.Message.
If no package is specified, the file's contents belong to the default root package.
To use an entity from a file in a different package, use an import directive.
Apart from the default imports, each file may declare its own imports.
Import one specific declaration so it can be used without qualification:
import org.example.Message // Message is accessible without qualification
fun main() {
val message = Message("Hello")
println(message.text)
}You can import everything accessible within a scope — a package, class, or object:
import org.example.* // Everything in 'org.example' is accessible
fun main() {
printMessage()
val message = Message("Hi")
}If two imported names clash, use the as keyword to locally rename one of them:
import org.example.Message // Message refers to 'org.example.Message'
import org.test.Message as TestMessage // TestMessage refers to 'org.test.Message'
fun main() {
val a = Message("from example")
val b = TestMessage("from test")
}The import keyword is not limited to classes. You can import any of the following declarations,
whether they come from a package, a class, an object, or an enum:
- Top-level functions and properties declared directly inside a package:
import org.example.printMessage // Top-level function import org.example.VERSION // Top-level property
- Functions and properties from object declarations:
import org.example.Config.DEFAULT_TIMEOUT // Property from an object import org.example.Config.loadSettings // Function from an object
- Members of a companion object, referenced through the enclosing class name:
import org.example.MyClass.create // Refers to MyClass.Companion.create
- Enum constants:
import org.example.Color.RED import org.example.Color.GREEN
- Nested and inner classes:
import org.example.Outer.Nested
Kotlin imports several packages into every file by default:
- kotlin.*
- kotlin.annotation.*
- kotlin.collections.*
- kotlin.comparisons.*
- kotlin.io.*
- kotlin.ranges.*
- kotlin.sequences.*
- kotlin.text.*
- kotlin.math.*
Additional packages are imported based on the target platform:
-
JVM:
-
JS:
The ability to import an entity depends on its visibility modifiers:
publicentities can be imported anywhere.internalentities can be imported only within the same module.protectedentities cannot be imported.- Top-level
privateentities are only accessible within their declaring file. - Other
privateentities cannot be imported.