Search⌘ K
AI Features

Understanding Kotlin Code Structure

Learn Kotlin code structure focusing on file and class organization, package usage, naming conventions, and unique language features such as package-level functions, optional main arguments, and no need for static modifiers or semicolons. Understand how these elements combine for concise and readable Kotlin code.

Unlike Java, there’s no strong relationship between the file name and class name in Kotlin. We can put as many public classes in our file as we want, as long as the classes are related to one another and the file doesn’t grow too long to read.

Naming conventions

As a convention, if our file contains a single class, we name our file the same as our class. If our file contains more than one class, then the filename should describe the common purpose of those classes. We use Camel case when naming our files, as per the Kotlin coding convention. The main file in our Kotlin project should usually be named Main.kt.

Packages

A package is a collection of files and classes that all share a similar purpose or domain. Packages are a convenient way to have all our classes and functions under the same namespace, and often in the same ...