Packages

A source file may start with a package declaration:

  1. package org.example
  2. fun printMessage() { /*...*/ }
  3. class Message { /*...*/ }
  4. // ...

All the contents (such as classes and functions) of the source file are contained by the package declared. So, in the example above, the full name of printMessage() is org.example.printMessage, and the full name of Message is org.example.Message.

If the package is not specified, the contents of such a file belong to the default package that has no name.

Default Imports

A number of packages are imported into every Kotlin file by default:

Additional packages are imported depending on the target platform:

Imports

Apart from the default imports, each file may contain its own import directives. Syntax for imports is described in the grammar.

We can import either a single name, e.g.

  1. import org.example.Message // Message is now accessible without qualification

or all the accessible contents of a scope (package, class, object etc):

  1. import org.example.* // everything in 'org.example' becomes accessible

If there is a name clash, we can disambiguate by using as keyword to locally rename the clashing entity:

  1. import org.example.Message // Message is accessible
  2. import org.test.Message as testMessage // testMessage stands for 'org.test.Message'

The import keyword is not restricted to importing classes; you can also use it to import other declarations:

Visibility of Top-level Declarations

If a top-level declaration is marked private, it is private to the file it’s declared in (see Visibility Modifiers).