Package

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.

Creating a Package

To create a package, you choose a name for the package and put a package statement with that name at the top of every source file.

The package statement must be the first line in the source file. There can be only one package statement in each source file.

Naming Conventions

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

Companies use their reversed Internet domain name to begin their package names. For example, com.example.mypackage for a package named mypackage created by a developer at example.com.

Using Package Members

Referring to a Package Member by Its Qualified Name

In this example, we use Date class in the package java.util.

Importing a Package Member

Now you can refer to the Rectangle class by its simple name.

Importing an Entire Package

To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

Now you can refer to any class or interface in the java.util package by its simple name. In this example, they’re Date & Calendar.