Packages, importing, and access
As you already know, a package name reflects a directory structure, starting with the project directory that contains the .java
files. The name of each .java
file has to be the same as the name of the top-level class declared in it (this class can contain other classes). The first line of the .java
file is the package statement that starts with the package
keyword, followed by the actual package name – the directory path to this file in which slashes are replaced with dots.
A package name and the class name together compose a fully qualified class name. It uniquely identifies the class but tends to be too long and inconvenient to use. This is when importing comes to the rescue by allowing specification of the fully qualified name only once, and then referring to the class only by the class name.
Invoking a method of a class from the method of another class is possible only if a caller has access to that class and its methods. The public...