Working With Files
Part of the Object Oriented Programming section of Coddy's Kotlin journey. Lesson 1 of 57.
Real programs are split into several files. All the .kt files of a Kotlin program are compiled together, so a function or class declared in one file can be used in any other file of the same package without an import:
// Greeting.kt
fun greeting(name: String) = "Hello, $name!"
// Main.kt
fun main() {
println(greeting("Ada"))
}Running this program prints Hello, Ada!: main in Main.kt calls the function that lives in Greeting.kt.
A Kotlin file holds declarations only: classes, functions, properties. Statements such as println(...) must be inside a function. The program starts at fun main(), which by convention lives in Main.kt:
// Greeting.kt
fun greeting(name: String) = "Hello, $name!"
// println("hi") // error: statements must be inside a function
// Main.kt
fun main() {
val name = readLine() ?: "friend"
println(greeting(name))
}The usual layout is one class per file, named after the class: Dog.kt holds Dog, Account.kt holds Account. In this section's challenges, your code goes in those files and Main.kt holds the supplied input/output code, which you usually cannot edit. The editor shows each file as a tab.
Dog.kt // class Dog(...) { ... }
Account.kt // class Account { ... }
Main.kt // fun main(): reads the input and prints the resultsVisibility follows files too. A top-level declaration marked private is visible only inside its own file, while the default, public, makes it visible in every file:
// Secret.kt
private const val SALT = "x7" // only this file can see it
fun hashed(s: String) = s + SALT
// Main.kt
fun main() {
println(hashed("pw")) // fine: hashed is public
// println(SALT) // error: SALT is private in Secret.kt
}Challenge
EasyThe program has two files. main in Main.kt reads names and prints the result of greeting(name). Complete greeting in Greeting.kt so it returns Hello, Ada! for the name Ada, or Hello, friend! when the name is empty.
Your code goes in Greeting.kt. Main.kt holds the supplied input/output code and cannot be edited.
Try it yourself
fun main() {
// Supplied input/output code: keep it as it is
val input = generateSequence(::readLine).toList()
for (name in input) println(greeting(name))
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Classes and Objects
Working With FilesDefining ClassesPrimary Constructorsinit Blocks and ValidationSecondary ConstructorsRecap - Library Card4Inheritance
Open Classes and SubclassesOverriding Memberssuper and ConstructorsAbstract ClassesRecap - Staff Directory2Properties and Encapsulation
Custom AccessorsVisibility ModifiersPrivate Setterslazy and lateinitRecap - Bank Account5Interfaces
Declaring InterfacesDefault MethodsMultiple InterfacesInterfaces as TypesRecap - Payment Methods8Generics
Generic FunctionsGeneric ClassesType ConstraintsVariance With in and outRecap - Priority Queue11Project: Library Management
Books and MembersBorrowing BooksPractice on your own: Kotlin playground