Packages & Imports

Packages Are Folders for Classes

A package groups related classes together — like a folder. org.wpilib.command3 is a real WPILib package holding the command-based framework's classes (Ch.25), and org.wpilib.hardware.discrete holds DigitalInput and DigitalOutput. Package names match folders on disk: a class in the package frc.robot.subsystems lives in a frc/robot/subsystems/ folder.

Importing a Class

To use a class from another package, import it at the top of your file:

package frc.robot.subsystems;

import org.wpilib.hardware.discrete.DigitalInput;
import org.wpilib.hardware.rotation.Encoder;

public class Arm { ... }

The package line at the top says where this file lives; each import brings in one class from somewhere else so you can use it by its short name (Encoder) instead of its full path. The order is always the same: package first, then the imports, then the class.

Other Ways to Reach a Class

  • Fully-qualified name — skip the import and write the whole path where you use the class: org.wpilib.system.Timer clock = new org.wpilib.system.Timer();. Legal, just long.
  • Wildcard import — import org.wpilib.hardware.discrete.*; imports every class in that one package (not the packages inside it).
  • Static import — import static java.lang.Math.sqrt; lets you call one static method without its class name: sqrt(9.0) instead of Math.sqrt(9.0) (static methods are Lesson 4.2).
  • Same package, no import — a class in the same package as your file can be used by its short name with no import at all.

One Package Needs No Import

java.lang is special — its classes (String, System, Math, and a handful of others) are always available, in every Java file, with no import needed. That's why you've been able to use System.out.println and String this whole time without importing anything — and why Math (Lesson 4.3) needs no import either.

Java 25 note: Java 25 adds import module java.base;, which imports every public class in Java's core library at once, and a compact source file (one that's just void main() { ... } with no class line around it) gets that import automatically. On Java 17 neither exists — import classes one at a time as shown above.

Running a file that has a package line: keep it in the folder its package names. On Java 22 and later, java SelfTest.java refuses to run a file whose package line doesn't match the folder it's in.

Common Pitfalls

  • Forgetting an import. If a class isn't in java.lang and isn't in your own package, using it without importing it is a compile error.
  • Importing something you don't use. Not a functional bug, but most IDEs will flag it — it's just clutter.

Key Takeaways

  • A package groups related classes together, the way a folder groups related files — and its name matches the folder path on disk.
  • import brings a class from another package into your file so you can use its short name.
  • You can also write a fully-qualified name, import a whole package with .*, or import static a single static member; classes in your own package need no import.
  • java.lang (which includes String, System, and Math) never needs an import — it's always available.

Derived from csawesome-2026: unit-1-using-objects-and-methods/1.07-apis-and-libraries.md (CSA 1.7 §1.7.2)