Loading published curriculum…

04 · Java language in depth / LESSON 10 OF 102

Packages, imports, and access modifiers

A package gives a class a namespace; access modifiers define who may use its implementation.

Before this lesson: Threads and shared state

Understand the topic

A public type can cross package boundaries. Package-private types stay within a package. private hides a member inside its declaring class; protected also supports subclass access, with additional rules across packages. An import shortens a name; it neither downloads code nor creates an object.

Put com.example.tasks classes under com/example/tasks. Compile with -d out so javac creates package directories. Run using the fully qualified class name and out as the classpath root.

EXAMPLE · JAVA

com/example/tasks/Hello.java

package com.example.tasks;
public class Hello {
  public static void main(String[] args) {
    System.out.println("Package ready");
  }
}

What to expect

javac -d out com/example/tasks/Hello.java followed by java -cp out com.example.tasks.Hello prints Package ready.

YOUR TURN

Put it into practice

  1. 01Create domain and application packages.
  2. 02Expose a public method while keeping its helper package-private. Attempt access from both packages.
  3. 03Verify: Compilation succeeds for the public API and rejects access to the hidden helper.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

A package gives a class a namespace; access modifiers define who may use its implementation.

  1. 01A public type can cross package boundaries. Package-private types stay within a package. private hides a member inside its declaring class; protected also supports subclass access, with additional rules across packages. An import shortens a name; it neither downloads code nor creates an object.
  2. 02Put com.example.tasks classes under com/example/tasks. Compile with -d out so javac creates package directories. Run using the fully qualified class name and out as the classpath root.
  3. 03Trace the example: javac -d out com/example/tasks/Hello.java followed by java -cp out com.example.tasks.Hello prints Package ready.

A mistake worth avoiding

The JVM cannot find Hello after compilation.

DEBUGGING ROOM

Something broke. Where do you look?

The JVM cannot find Hello after compilation.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does importing a class create an instance?

Choose one answer

BUILD IT YOURSELF

Create domain and application packages.

  1. 1.Create domain and application packages.
  2. 2.Expose a public method while keeping its helper package-private. Attempt access from both packages.
  3. 3.Reproduce this failure, then repair it: The JVM cannot find Hello after compilation.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does importing a class create an instance?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Compilation succeeds for the public API and rejects access to the hidden helper.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references