Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming usage

Packages and imports

4 minutes read

In Kotlin, we usually structure our projects into packages. Packages group classes, functions, and/or variables according to a particular use case or functionality.

In this topic, we will learn how to define packages and how to use them efficiently in our code.

Packages

In a package, we group classes, functions, and/or variables according to a specific use case or functionality. To define a package, we use a package header. A file can either belong to a package or not, and it may make use of none, one, or multiple packages. A package contains one or more Kotlin files, with files linked to a package using the same package header. One file or class can use zero or multiple packages. If the package is not specified, the contents of such a file belong to the default package with no name.

Kotlin packages do not require files to have any specific locations, the connection between a file and its package is established only via the package header.

In the following example, you can see how we define a package. All the elements within this package, whether they are in the same file or not (classes and functions), are encapsulated under that package. To be able to use them, we must indicate the complete name of the element, which includes the package name.

package packages

data class Person(val name: String, val age: Int)

fun sayHello() {
    println("Hello")
}

fun main() {
    val person = packages.Person("John", 30)
    packages.sayHello()
}

Imports

If we want to use code that is grouped or encapsulated within a package, we must import it. We can import either the entire package or some specific elements from that package. To import a package, we need to use the import directive.

In the following example, we import the entire allpackage package using the * modifier. On the other hand, we import only the Person class from the oneclass package.
This way, we can import only those parts of the package that we actually need and thus optimize their use in our code.

package packages

import allpackage.*
import oneclass.Person

fun sayHello() {
    println("Hello")
}

fun main() {
    val person = Person("John", 30)
    sayHello()
}

Default imports

There are a number of packages that are imported into every Kotlin file by default:

  • kotlin.*

  • kotlin.annotation.*

  • kotlin.collections.*

  • kotlin.comparisons.*

  • kotlin.io.*

  • kotlin.ranges.*

  • kotlin.sequences.*

  • kotlin.text.*

Some packages are imported depending on the target platform:

  • JVM:

    • java.lang.*

    • kotlin.jvm.*

  • JS:

    • kotlin.js.*

Import alias

Sometimes, we may need to import two elements that have the same name but belong to different packages, or import the same class and use it with two different names. The first option to solve this task is to use the complete package name and element name as an identifier. However, this option can be long and sometimes unproductive.

We can solve this situation by using an alias for our imported classes or functions. We do that with the as modifier. This way, we can create an alias for any collisions we have and work with our code in a much simpler way.

package packages

import packageone.Person
import packagetwo.Person as PersonAdvanced

fun main() {
    val person = Person("John", 30)
    val advanced = PersonAdvanced("John", 30)
}

Conclusion

In this topic, we have learned how to organize our code into packages and how to use Kotlin packages in the most productive way possible.

Now is the time to do some tasks to check what you have learned. Are you ready?

34 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo