1. Why do you even need import if there is package?
When a project is small, it can feel like import is “some kind of bureaucracy.” But as soon as you create a second package and a couple of utilities, you start writing spells like app.util.normalizeCommand(...). And it’s kind of fine… until you do it 20 times in one file. Then the code starts to look like the address book of a post office.
The idea of import is very simple: it lets you use a short name instead of a fully qualified name. A fully qualified name is an “address + name” separated by dots. For example, app.util.normalizeCommand. If the file has import app.util.normalizeCommand, then later you can write just normalizeCommand(...).
Let’s compare with a mini example.
Suppose we have a function in the app.util package:
package app.util
fun normalizeCommand(raw: String): String {
return raw.trim().lowercase()
}
Without an import, in another package you’d have to call it like this:
package app
fun main() {
val cmd = app.util.normalizeCommand(" HeLP ")
println(cmd) // help
}
With an import it’s more readable:
package app
import app.util.normalizeCommand
fun main() {
val cmd = normalizeCommand(" HeLP ")
println(cmd) // help
}
In plain human terms: import means “I’m going to use this name a lot in this file, let’s drop the extra prefixes.”
Fully qualified name instead of import: when it’s actually better
Sometimes you simply don’t want to add an import. For example, you use some function exactly once. Or you’re writing a learning example and want to show explicitly where the name came from. In such cases, a fully qualified name is a perfectly normal tool.
For example, you need a square root:
package app
fun main() {
val x = kotlin.math.sqrt(9.0)
println(x) // 3.0
}
This is a completely workable style. Moreover, sometimes it’s even more useful than an import: you see “the root from kotlin.math,” not “some sqrt that came from who-knows-where.”
A practical rule: if a name is used rarely, using the fully qualified name is fine. If it’s used often, imports save your eyes and your keyboard.
2. Where import lives and why it doesn’t “include a file”
There’s a common (and very understandable) misconception: “I did an import, so I included a file.” In Kotlin you import not a file and not even a “folder,” but a specific name (a function, class, object, property) that lives in another package.
Even more important: an import is local to a file. It only works inside the .kt where it’s written. In a neighboring file, you start over: its own package, its own import.
In Kotlin, the usual order in a file is:
- package ... (if you use packages)
- import ...
- declarations: functions, classes, etc.
Diagrammatically it looks like this:
┌──────────────────────────────┐
│ package app │
├──────────────────────────────┤
│ import app.util.normalize... │
│ import kotlin.math.sqrt │
├──────────────────────────────┤
│ fun main() { ... } │
│ fun helper() { ... } │
└──────────────────────────────┘
If you try to write an import in the middle of the file after fun main(), the compiler won’t be happy. Kotlin is strict here, like a teacher who caught you with a cheat sheet: “imports go at the top.”
Imports as a file’s “dependencies”
A useful habit is to look at the import list as “what this file needs in order to live.”
For example:
package app
import app.util.normalizeCommand
import app.format.formatAmount as prettyAmount
import kotlin.math.round as mathRound
It’s almost like the top tool rack in a workshop: “here’s a hammer, here’s a screwdriver, here’s electrical tape.” If there are too many imports and they feel random, the file is often doing too many jobs. If there are almost no imports but the file contains huge constructs like a.b.c.d.e.f(...), you’re probably suffering from missing imports (or the code should be split into functions and packages).
An IDE usually helps: it can add imports automatically, remove unused ones, and optimize the list.
3. import in a training app: extracting command normalization
From this point on, let’s imagine we’re writing a small console app called BudgetBuddy. It doesn’t use collections yet (that comes later), doesn’t use classes yet (that’s even later), but it can already read commands, normalize them, and execute simple scenarios.
Let’s make main live in app, and utilities live in app.util.
File src/app/util/Command.kt:
package app.util
fun normalizeCommand(raw: String): String {
return raw.trim().lowercase()
}
Now file src/app/Main.kt:
package app
import app.util.normalizeCommand
fun main() {
print("Enter a command: ")
val cmd = normalizeCommand(readln())
println("Command after normalization: $cmd")
}
Notice the “contract”: normalizeCommand takes a String and returns a String. It does not call readln() itself. That’s convenient: the function becomes pure and predictable, and main controls input/output.
4. import ...*: convenient, but turns code into a detective story
The star in an import (*) means: “import all available names from this package.” It looks like a cheat code:
import kotlin.math.*
After that you can write sqrt, round, ceil, floor without separate imports.
The problem is that a star makes code less obvious: you open a file and see round(...). But round — is that our round? Is it kotlin.math.round? Is it something else entirely? The larger the project, the more often you’ll answer that question not with your head, but by searching the project.
That’s why in learning projects and in most team code styles people prefer explicit imports:
import kotlin.math.sqrt
import kotlin.math.round
A star import is sometimes justified, but usually only in situations where you fully understand the context. For example, when a file is entirely about math and truly uses a dozen functions from kotlin.math. In typical application logic, a star often turns into “why is it called this and where did it come from?”
5. Name conflicts and alias
A name conflict is when in one file you end up with two candidates for the same short name.
The most realistic scenario: you wrote your own round function, and then you wanted to use kotlin.math.round. Or you wrote format, and then you found another format in the project. Kotlin won’t guess “which one you meant,” because guessing is a direct path to bugs like “it worked yesterday.”
Let’s intentionally create a conflict.
File src/app/util/Round.kt:
package app.util
fun round(x: Double): Int {
return x.toInt()
}
And now in Main.kt we want both ours and the math one:
package app
import app.util.round
import kotlin.math.round
fun main() {
println(round(3.6))
}
This code won’t compile: two imports with the same name round create ambiguity.
Kotlin has three reasonable strategies: use the fully qualified name, change the import list, or use an alias (an import with a nickname). Below is the most readable and “grown-up” option: alias.
Alias import: a local name via as
An alias import looks like this:
import kotlin.math.round as mathRound
It’s as if you’re saying: “in this file, I will call kotlin.math.round by the name mathRound.” Important: you didn’t rename the function in the library and you didn’t change the project. You just created a local nickname for the name inside this specific file.
Let’s rewrite Main.kt so it works and is clear:
package app
import app.util.round
import kotlin.math.round as mathRound
fun main() {
val x = 3.6
val a = round(x) // our version: Int, simple truncation
val b = mathRound(x) // kotlin.math version: Double, proper rounding
println("a = $a") // a = 3
println("b = $b") // b = 4.0
}
Here the code is readable even for a beginner: you can see that mathRound is “math rounding,” and round is something of ours.
Alias with two formatAmount: an example closer to a real app
With round the example is clear, but a bit artificial. In application code, conflicts more often happen with words like format, parse, normalize, printReport. In other words, perfectly normal names you’d want to use in different places.
Let’s make two amount-of-money formatters: one “raw” (just a number), the other “pretty” (with a currency). And we’ll intentionally give them the same name formatAmount to see alias in action.
File src/app/util/AmountFormat.kt:
package app.util
fun formatAmount(amount: Int): String {
return amount.toString()
}
File src/app/format/AmountFormat.kt:
package app.format
fun formatAmount(amount: Int): String {
return "$amount USD"
}
Now in Main.kt we want both: for debugging — the “raw” one, for the user — the “pretty” one.
package app
import app.format.formatAmount as prettyAmount
import app.util.formatAmount as rawAmount
fun main() {
val income = 1200
println("DEBUG: income=${rawAmount(income)}") // DEBUG: income=1200
println("For the user: ${prettyAmount(income)}") // For the user: 1200 USD
}
Note two things.
First: alias imports help not only to “resolve a conflict,” but also to add meaning. prettyAmount reads like “pretty formatting,” while rawAmount reads like “raw for debugging.”
Second: an alias only applies in the Main.kt file. In another file, you can choose different aliases — or not use alias at all if there are no conflicts.
6. BudgetBuddy mini refactor: making main thinner
Now let’s assemble a small “practical” piece that looks like a real application (but still without collections and OOP).
The idea is: main reads a command, normalizes it, and prints a response. Input/normalization utilities live in app.util. Formatting lives in app.format.
File src/app/util/Input.kt:
package app.util
fun readTrimmedLine(prompt: String): String {
print(prompt)
return readln().trim()
}
File src/app/util/Command.kt:
package app.util
fun normalizeCommand(raw: String): String {
return raw.trim().lowercase()
}
File src/app/format/Amount.kt:
package app.format
fun formatAmount(amount: Int): String {
return "$amount USD"
}
File src/app/Main.kt:
package app
import app.format.formatAmount
import app.util.normalizeCommand
import app.util.readTrimmedLine
fun main() {
val raw = readTrimmedLine("Enter a command (income/exit): ")
val cmd = normalizeCommand(raw)
if (cmd == "income") {
println("Your income: ${formatAmount(1200)}") // Your income: 1200 USD
} else if (cmd == "exit") {
println("Bye!") // Bye!
} else {
println("Unknown command: $raw") // example: Unknown command: qwe
}
}
What matters: Main.kt became shorter and clearer. It doesn’t have to know exactly how we trim a string or how we format an amount. It simply “brings names into scope” via import and uses them as building blocks.
7. Common mistakes when working with import and alias
Mistake No. 1: thinking import applies to the whole project.
An import is a setting for a specific file. If you added import app.util.normalizeCommand in Main.kt, that doesn’t mean it also appeared “by itself” in Report.kt. Each file has its own set of imports — and that’s normal: files should be independent and honestly declare what they need.
Mistake No. 2: turning import ...* into the default style.
The star saves a couple of lines today, but it often creates confusion tomorrow: it becomes unclear where a name came from and why it’s that name. This is especially painful with name conflicts, when you suddenly discover that format exists “somewhere else,” and you didn’t know about it. It’s better to start with explicit imports and use * consciously and rarely.
Mistake No. 3: trying to “rename a function in the project” via alias imports.
import ... as ... renames the name only inside a single file. It’s not a refactor and not a rename of the declaration. If you imported kotlin.math.round as mathRound, you didn’t create a new function and didn’t change the library — you just gave it a local nickname so the code becomes unambiguous.
Mistake No. 4: “fixing” a name conflict by renaming randomly instead of by meaning.
When a conflict appears, a beginner often creates aliases like round1 and round2. The code compiles, but it’s impossible to read. It’s better to choose aliases that explain the role: mathRound, prettyAmount, rawAmount, cliNormalize. That’s not pedantry — it’s an investment so that a week later you don’t hate your past self.
Mistake No. 5: leaving junk (unused) imports.
Sometimes after experiments you have imports left that are no longer needed. This doesn’t break the program, but it clutters the file and hides the code’s real dependencies. A good habit is to periodically remove unused imports (the IDE usually can do this automatically), especially if you actively refactor and move functions across packages.
GO TO FULL VERSION