Package of classes, interfaces, fields, methods and annotations, usually making them available for different applications. It is also useful for creating better organized code.
From a package methods, classes, sub-package or the whole package can be imported. After importing, the data in package can be used.
Naming of elements can be the same in case they are in different packages.
Types of packages:
built-in (e.g. java.lang, java.io)
user-defined
In a project packages organized into a tree structure which projects into a folder structure.
FIGURE OF ORGANIZING PACKAGE STRUCTURE:
In classes and interfaces the 1st line must contain the package name, following the above structure:
on 1st level: package package_name;
on 2nd level: package package_name.sub_package_name;
After the package identifier, imports and the class header line come.
The class name will be the java file name as usual.
The package name must not be equal with any class or interface names within.
Within a package, use the next class declarations:
[protected] class ClassName {...} //a package helper class, visible for package classes only
public class ClassName {...} //visible from outside the package as well
For console-based compilation of the whole package, use:
javac <path>\*
Error message appears in case of need of overwriting.
The naming conventions for packages is as follows:
package names are low-cased
different levels are separated by . sign
the first two levels are for marking the website of developer company in reversed order
in case the given package has different versions (e.g. by regions), this name is marked here
in case of more words on the same level, do not use separator between
finally the name of the given package or package structure
Some rules:
in case of illegal character or - sign in the package name, replace it with underscore (_)
in case of starting with numerical character, insert an underscore before the numbers
in case a word on a package level is a Java keyword, place an underscore after it
Examples are below for a company at example.com website.
All classes and interfaces in the working directory of the application form an unnamed package.
Such package members are not marked with the package statement in their first lines.