Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to Kotlin  ›  Lesson

Packages, Files and Imports

Code Organization 12 minKotlin code is grouped into packages declared at the top of each file, one file may hold several top-level declarations, and imports bring names from other packages into view.

Packages, Files and Imports

The café app starts as one file. A month later it has orders, a menu, loyalty points and receipts, and scrolling to find anything takes forever. Kotlin gives you packages and imports to keep a growing codebase tidy without much ceremony.

The rule

package says where I live; import says whom I borrow.

A package is a named group of declarations. The package line comes first in a file, before anything else except comments:

package cafe.orders

import cafe.menu.Menu
import kotlin.math.roundToInt

class Order(val drink: String, val cups: Int)

fun orderTotal(order: Order, menu: Menu): Int =
    (menu.priceOf(order.drink) * order.cups).roundToInt()

What each part tells you:

  • package cafe.orders — everything in this file has the full name cafe.orders.…, so Order is really cafe.orders.Order.
  • import cafe.menu.Menu — lets you write Menu instead of the full name. import cafe.menu.* brings in everything from that package.
  • Several top-level declarations per file. Unlike Java, Kotlin does not demand one public class per file. Functions and constants can live at the top level, outside any class.
  • File name is free. The file could be Orders.kt even though it holds Order and orderTotal. Naming it after its main class is a convention, not a rule.

The folder layout usually mirrors the package: src/main/kotlin/cafe/orders/Orders.kt. Kotlin allows a mismatch, but the convention helps people find things.

Some packages, such as kotlin.*, kotlin.collections.*, kotlin.text.* and kotlin.io.*, are imported automatically. That is why println and listOf work without any import line.

If two imports share a name, rename one with as: import cafe.menu.Item as MenuItem.

The shortcut

To read any name in a file, look up: first the declarations in the file itself, then the package line (same package, no import needed), then the import lines, then the default imports.

Where does `Menu` come from?Step 1 of 4
  1. Look in this file

    Orders.kt declares Order and orderTotal, but not Menu.

A typical mistake is putting import above package. The package line must come first.

Drills

Answer fast — trust the rule.

quick drill

Which line must come first in a Kotlin file, before any imports?

quick drill

A file in package cafe.orders declares class Order. What is its full name?

quick drill

Which statement about Kotlin files is true?

quick drill

Two packages both have a class Item. How can one file use both?

Remember

  • package says where a file lives; import says what it borrows.
  • One file can hold many top-level declarations; the file name is free.
  • Read names upward: this file, this package, imports, default imports.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOrganise the café code

The café app has order code and menu code. Describe how you would split them into packages and files, and show how a file in the orders package would use a Menu class from the menu package.