Skip to content

Chapter 17 of 37

Packages and Access Control

Organize types, imports, classpaths, visibility, and package-level APIs.

28 minutes 10 quick checksBy Subha Prasad
Lesson 17 of 37Course navigation

Lesson content

Read, practise, then check your understanding

Packages provide namespaces and a visibility boundary. The source directory normally mirrors the package name. Imports shorten type references at compile time; they do not load code or include files.

Organizing an API

package com.example.orders;

import java.time.Instant;
import static java.util.Objects.requireNonNull;

public record Order(String id, Instant createdAt) {
    public Order {
        requireNonNull(id);
        requireNonNull(createdAt);
    }
}

Use reversed-domain names to avoid collisions. Wildcard imports do not include subpackages and can make name sources less obvious. Package-private types and members form an internal implementation surface; public types are long-lived contracts. The classpath locates unnamed-module classes and resources, while the module path resolves named modules. Avoid split packages across libraries and keep dependencies flowing from stable APIs toward replaceable implementations.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes package?
Which Java term matches this description: A named namespace organizing types and controlling package access.
Which statement best describes import?
Which Java term matches this description: A compile-time convenience for referring to a type by simple name.
Which statement best describes fully qualified name?
Which Java term matches this description: A type name prefixed by its complete package path.
Which statement best describes static import?
Which Java term matches this description: An import allowing static members to be referenced without type qualification.
Which statement best describes classpath?
Which Java term matches this description: The locations searched for compiled classes and resources.

0 of 10 checks passed

Your progress is saved on this device.

Packages and Access Control | Java Lesson | Subha Prasad