Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management.
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management.
- java.lang - bundles the fundamental classes
- java.io - classes for input , output functions are bundled in this package
The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.
It is common practice to use lowercased names of packages to avoid any conflicts with the names of classes, interfaces.
if a class wants to use another class in the same package, the package name does not need to be used.
if not,
- The package can be imported using the import keyword and the wild card (*). For example:
import payroll.*;
A class file can contain any number of import statements. The import statements must appear after the package statement and before the class declaration.