Packages

Kotlin makes use of packages the same way that Java does: as a namespace for classes. And, as with Java, by default a class is not in a package. Most Kotlin files in an actual project — as opposed to little REPL experiments — will be in a package.

The syntax for setting up a Kotlin package is nearly identical to that of Java: put a package line at the top of the file:

package bar.goo

class Foo

val foo = Foo()

What differs, compared to Java, is just the lack of the semicolon at the end of the package statement that the equivalent Java package statement would require.

Note that while having packages is very common in Kotlin projects, Klassbook does not use them, and neither do most other REPLs.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.