Package layout and dependency boundaries
What you will learn
The source root contains Java source files; the classpath points the compiler or launcher at locations containing compiled classes and libraries. Neither is the package itself. A public facade can hide implementation details without adding an external dependency or build system.
- Compile sources into a package-oriented class output tree and launch using a qualified class name.
- Separate a public facade from internal helpers and diagnose a missing classpath entry.
Use a fresh lab folder for each example, JDK 21 or compatible newer JDK, no external libraries. Create the listed files under src; commands are run from that lab folder. The source compatibility target is Java 21; Java 25 execution is not yet verified.
Build a public pricing facade and hidden helper
package demo.pricing;public class Price { public static int total(int quantity) { return Rules.unitPrice() * Rules.checked(quantity); }}package demo.pricing;final class Rules { static int unitPrice() { return 4; } static int checked(int quantity) { if (quantity < 0 || quantity > 100) throw new IllegalArgumentException("Quantity out of range"); return quantity; }}package demo.app;import demo.pricing.Price;public class Main { public static void main(String[] args) { System.out.println(Price.total(3)); System.out.println(Price.total(0)); }}Compile and run:
javac --release 21 -d out src/demo/pricing/Price.java src/demo/pricing/Rules.java src/demo/app/Main.javajava -cp out demo.app.MainExpected output:
120Price is public; Rules is package-private. Main depends on Price, not the implementation helper. Both zero and a positive quantity work.
Source roots, class output and dependency direction
The javac -d out option places generated classes under out according to their package names. For demo.app.Main the expected class file is out/demo/app/Main.class. Use java -cp out demo.app.Main, not a file path or a .class suffix. At compile time, javac -cp out can find existing compiled dependencies when the required source files are not included. At runtime, java -cp out must include the same needed classes; compiling successfully does not guarantee a correct launch environment. Multiple classpath entries use a colon on Unix-like systems and a semicolon on Windows; use a single root here for portability. Package declarations organize names, not Maven artifact coordinates (group/artifact/version), and not Java modules. A package-private helper can be replaced without widening the public API, although changes to outputs still require tests. If the launcher cannot find the main class, check the exact name and the root containing the package tree before changing source imports. Use a fresh out directory in each fixture so stale classes do not hide missing files.
Compile a library before its client
package demo.pricing;public class Price { public static int total(int quantity) { return Rules.unitPrice() * Rules.checked(quantity); }}package demo.pricing;final class Rules { static int unitPrice() { return 4; } static int checked(int quantity) { if (quantity < 0 || quantity > 100) throw new IllegalArgumentException("Quantity out of range"); return quantity; }}package demo.app;import demo.pricing.Price;public class Main { public static void main(String[] args) { System.out.println(Price.total(5)); }}Compile and run:
javac --release 21 -d out src/demo/pricing/Price.java src/demo/pricing/Rules.javajavac --release 21 -cp out -d out src/demo/app/Main.javajava -cp out demo.app.MainExpected output:
20Compile the two pricing files to out first; compile Main with out on its compile-time classpath, then run with out on the runtime classpath. Source and class roots are distinct.
Replace an internal rule without changing the client API
package demo.pricing;public class Price { public static int total(int quantity) { return Rules.unitPrice() * Rules.checked(quantity); }}package demo.pricing;final class Rules { static int unitPrice() { return 6; } static int checked(int quantity) { if (quantity < 0 || quantity > 100) throw new IllegalArgumentException("Quantity out of range"); return quantity; }}package demo.app;import demo.pricing.Price;public class Main { public static void main(String[] args) { System.out.println(Price.total(3)); try { Price.total(101); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } }}Compile and run:
javac --release 21 -d out src/demo/pricing/Price.java src/demo/pricing/Rules.java src/demo/app/Main.javajava -cp out demo.app.MainExpected output:
18Quantity out of rangeThe helper changes the unit price from 4 to 6. The public signature stays the same, but the observable price changes; verify that this is a deliberate business change. Validation still rejects 101.
Common mistakes
Do not put out/demo/app on the classpath for demo.app.Main: the launcher would look for the package path relative to the wrong root. Do not rely on an inherited CLASSPATH or an IDE-only launch setup.
Practice
Build demo.tax.Tax with public static total(int units) and a package-private Rate helper returning 3. Accept 0–10 units; reject values outside that range before multiplication. Compile Tax and Rate first, then Main separately. Print totals for 0 and 4, catch the failure for 11, and explain why java -cp out demo.app.Main works.
Hints:
- Keep the helper in demo.tax and expose only Tax.total.
- Use javac -cp out -d out for the client and a fresh output directory for each run.
Related reference topics: Java javac -d output directory, Java classpath, Java package-private helper and public facade. Next: Enums, records and immutable data.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Package layout and dependency boundaries — knowledge check
Apply package, visibility and classpath rules in Java 21-compatible fixtures.
Given out/demo/app/Main.class, what command launches the class?
What does javac -d out do?
Why pass -cp out when compiling Main after Price?
What happens if the runtime classpath omits the main class?
Which design keeps implementation changes local?