Organizing Code into Packages
Java packages together related classes, interfaces, enums (a data type that contains a data type that contains a fixed group of constants), and annotations (contain metadata). In other words, a package is a collection of Java types brought together under a common name. Using a common name makes it easier to find code in larger projects, and helps to keep your code separate from other, perhaps similar, code. For example, more than one package might contain a class named Rectangle
, so referring to the appropriate package will allow you to specify which Rectangle
class you're looking for. Packages allow you to organize your code, which becomes more and more important as you work on larger and larger applications.
Java's API includes hundreds of classes divided into packages, such as java.math
and java.net
. As you'd expect, java.math
has mathematics-related classes, and java.net
has networking-related classes.
Importing Classes
When...