Every module declares a unique root package, that contains all the packages declared in this module.
A source file may start with a header package declaration (see the grammar):
All the contents of the source file are (directly or indirectly) contained by its header package. If the header is not specified, the module's root package is used.
Besides the default imports declared by the module, each file may contain its own import directives.
Syntax for imports is described here.
One can import either a single name, e.g.
or all the accessible contents of a scope (package, class, object etc):
If there is a name clash, one can disambiguate by using as keyword to locally rename the clashing entity:
Consider the following example (syntax is fictional):
At the line marked "Problem" we have the following potential ambiguity: what does a stand for: the outer package a or the inner one? The rule here is that names are always relative: a is resolved in its scope of occurrence that makes it mean the inner package a, so this code does not compile.
To fix it, we need an absolute name. These start with the package keyword:
In this example, we assumed that the root package of our module is named root.
Absolute names may be used in imports as well: