Unit 2 · Lesson 414 minAcademic review pending

Folders for classes, with a compiler that checks them

As a project grows past a handful of classes, two unrelated classes both named Node becomes a real problem. Packages are Java's answer — a namespace, backed by the actual folder structure on disk.

Choose explanation

After this lesson

You should be able to

  • Declare a class inside a package and explain how that maps to a folder.
  • Import a class from another package and explain what import actually does.
  • Explain what CLASSPATH is, in one sentence.
01

A package is a namespace, and a folder

A package statement at the top of a Java file, package school.students;, does two things at once: it groups the class into a logical namespace, so school.students.Student and library.catalog.Student can coexist without colliding, and it requires the file to physically live in a school/students folder on disk. Unlike a C project, where file location and naming were entirely up to you, Java enforces this correspondence — the compiler will reject a mismatch.

This solves the exact naming-collision problem CS105ES and CS205ES never had to face, because those courses were always small, single-file programs. Once a real project has hundreds of classes written by different people, packages are what keeps two independently-written Node classes from ever needing to know about each other at all.

The package declaration and the folder must match
// file: school/students/Student.java
package school.students;

public class Student {
    // ...
}
02

import: a shortcut, not magic

import school.students.Student; at the top of another file lets you write Student instead of the full school.students.Student every time you use it in that file. Nothing is copied and nothing is loaded early — import is purely a name-shortening convenience for the compiler, resolved entirely at compile time. You could skip it entirely and always write the fully-qualified name, it would just be verbose.

CLASSPATH is the list of locations — folders and compiled jar files — where the Java runtime looks when it needs to find a compiled class by name. Think of it as the Java equivalent of telling gcc where to find a header file it needs, except CLASSPATH matters both when compiling and when actually running the program.

Try it yourself

Write the package declaration and folder path for a class Book that should live in a logical namespace called library.catalog. Then write the import statement another file, in a different package, would need to use Book without its full name.

Need a hint?

The folder path mirrors the package name exactly, with dots becoming slashes.

Check the worked solution

package library.catalog; places Book at library/catalog/Book.java on disk — the dot-separated name and the slash-separated path carry exactly the same information, just in two different notations required by two different contexts, one for the compiler's namespace and one for the filesystem.

// file: library/catalog/Book.java
package library.catalog;

public class Book {
}

// in another file, another package:
import library.catalog.Book;

Quick check

What does the statement import school.students.Student; actually do?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Defining, Creating and Accessing a Package, Understanding CLASSPATH, importing packages.

Maps to course outcome CO1.