In Kotlin, packages are collections of related functions, classes, objects, and type definitions. They are used to structure code into logical units and help to avoid naming conflicts between identically named functions and classes in different parts of a program.

Organizing code in the form of packages is particularly important in large software projects. Packages help keep the code clear. They make maintenance and finding specific pieces of code easier. They also enable a clear separation of different functional areas and promote the reusability of code.

Creating a package in Kotlin

A package is sent with the keyword package declared. The package definition is placed at the beginning of a file. All code in this file is then considered part of this specific package.

In principle, multiple packages can be declared in the same file. But this is not common in practice. Typically, a file corresponds to a package to improve code clarity and maintainability.

Accordingly, we create a separate file for our project called Finance, in which all classes and functions relevant to the processing of financial operations are defined.

To create a new Kotlin file, we click in kotlin-Right-click the folder and select it New and then Kotlin class/file. We then enter the desired file name:

Creating a new file in Kotlin project

The newly created file finance.kt opens in the editor:

File for package in Kotlin

We then define the following code in it:

In this code there is a Finance Package defines a class Invoice with a property amount of the type Double contains. This class represents an invoice with a specific amount.

There is also a function pay, which accepts an Invoice object and an account number as a string and prints a message to the console confirming that the invoice has been paid and which account the money was debited from.

Importing a package into Kotlin

Suppose we want the functionalities of this package in the file main.kt to use. To include entities from the package, the import-instruction can be used. There are various ways to integrate functionalities from the package. For example, you can the entire package include:

A period and an asterisk are placed after the package name, which means that all types from this package are imported.

Since at the beginning of the file all types from the package Finance have been imported, we can use the class Invoice and the function pay in the main-Use function. After executing the code, we get the following output in the console:

You can also import types defined in the package individually. Importing individual types from a package is done using the syntax import .. In this specific case you can use the class Invoice as well as the function pay import as follows:

Pseudonyms in Kotlin

In Kotlin, as in many other programming languages, pseudonyms (or aliases) can be used to rename or reassign classes, functions, or other elements. This feature is particularly useful for increasing code readability, avoiding naming conflicts, or simply referencing certain elements by a shorter or more specific name.

For example, when importing a class or function from a package, you can create an alias using the keyword as define. 

Pseudonyms can be particularly useful when importing elements with the same name from different packages. For example, let's say in our project there is another package called mailbox, in which incoming mail including invoices is registered accordingly and prepared for further processing. So, let's create a new file for this mailbox.kt and define the following code:

In this code we have a package called mailbox created. The class Invoice represents an invoice and takes the invoice total (amount) as a parameter. The function invoice registers (in a simplified form) the incoming invoice and prints a message on the console. 

Standard packages in Kotlin

Kotlin has a number of built-in packages that are available by default in every Kotlin file. These packages include basic functionality and classes that are commonly used in many programs. Here is an overview of some of these packages:

  • kotlin.*: This package contains the most basic classes and functions of Kotlin, such as basic data types (Int, String, etc.), control structures and basic functions.

  • kotlin.annotation.*: This package defines annotations that can be used in Kotlin to provide additional information about program parts.

  • kotlin.collections.*: This package contains classes and functions for working with collections such as lists, sets and maps.

  • kotlin.comparisons.*: Here you will find help functions for comparing objects, which is particularly useful when sorting collections.

  • kotlin.io.*: This package provides functions and classes for input and output operations, such as reading and writing files.

  • kotlin.ranges.*: This package contains classes and functions that simplify working with ranges and intervals, such as the Range classes.

  • kotlin.sequences.*: Sequences in Kotlin are similar to streams in Java and provide a way to lazily process elements.

  • kotlin.text.*: This package contains functions and classes for working with text and strings, such as string manipulation and regex operations.

Since these packages are automatically available in every Kotlin file, it is unnecessaryto import them explicitly. This feature makes development in Kotlin more convenient and reduces the need for extensive import lists at the beginning of files.

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked