How packages organize code

Declare a package

The package declaration is the first non-comment statement. Its directory path should match the dotted package name.

package com.example.school;

public class Student {
}

Import a type

An import lets source code use a type's short name. Types in java.lang are imported automatically.

import java.util.ArrayList;

ArrayList<String> topics = new ArrayList<>();

Use qualified names

A fully qualified name identifies a type without an import and resolves conflicts between same-named classes.

java.time.LocalDate today = java.time.LocalDate.now();

Follow package conventions

  • Use lowercase package components.
  • Begin reusable code with a reversed domain you control.
  • Keep closely related classes in the same package.
  • Compile and run from the source root so package paths resolve correctly.