7) Packages Lesson

What are Java Packages?

5 min to complete · By Ryan Desmond

In Java, a package is a group of related classes, interfaces, enumerations, and annotations.

Why Use Java Packages?

Packages in Java have two primary uses:

  1. They offer a way of organizing related classes together. Classes in a package need to be accessed by their package name, so it is a way to group classes together by a shared name or shared functionality.
  2. Packages participate in Java’s access control. You have discussed how access modifiers restrict use from class to class, and you will now see how they affect access within and across packages.

How to Use Java Packages?

Java Packages are quick to define, but their location and hierarchy are important factors in their functionality.

Define a Package

To create a package, put the package keyword followed by the name of the package at the top of a Java file. For example:

package package1;
class Example{
    // class contents
}

Class Example is located in the package package1.

Create the Package Hierarchy

Packages can be organized in a hierarchy.  Packages are separated by the dot operator to create a hierarchy. For example:

package package1.package2.package3;

In this scenario, package1 must contain package2, and package2 must contain package3.

Screenshot showing IntelliJ with a few empty packages

Take a look at the image below. Notice the package definition line in the code and how it corresponds directly to the file structure in the left navigation.

Highlighting the example package definition alongside the directory structure in IntelliJ

Ensure the Package Location

Package hierarchy must match the file path in your directory. For example, the above package hierarchy must match the file structure: package1/package2/package3. So, any files in package3 must be in the file package3 in your directory. This will also correspond directly to the file system that contains the project, as you can see in the image below.

Graphic of folder structure on your computer

Compile and Run a Class

To compile and run a Java file in a package, you must be able to see the highest-level package. For example, to compile and run the image above, you need to be in the folder where you can see the codingnomads folder (which is the root of the package, as you can see in the image).

javac codingnomads/examples/oop/inheritance/CarController.java

To run that application:

java codingnomads/examples/oop/inheritance/CarController  

Summary: What are Java Packages?

  • A package is a group of related classes, interfaces, enumerations, and annotations
  • Java packages offer a way of organizing related classes together
  • Packages participate in Java’s access control
  • The chosen location and hierarchy of the package are important factors
  • A class in Java can be run directly from the command line using the java command

Syntax

Here's the syntax for creating a package, where you can substitute the variables starting with your_ with your values.

package your_package;
class Your_Class{
    //...
}