Part 07 / Topic 2 of 7
Packages in practice: folders must match the dots
The rule: if a class declares package billing.util;, its .java file must live in a folder path billing/util/ under some root folder, and after compiling, its .class file must live in billing/util/ under the output folder. Dots in the package name become slashes in the folder path.
Think of the package name as a postal address and the folder structure as the actual street layout — the postman (the java launcher, and javac when it goes looking for a file you didn't list) can only deliver if the address matches a real path.
Here is a tiny three-file program laid out correctly:
$ find . -type f | sort
./Main.java
./billing/Invoice.java
./billing/util/Formatter.java
Invoice.java (package billing):
package billing;
public class Invoice {
private final String customer;
private final double amount;
public Invoice(String customer, double amount) {
this.customer = customer;
this.amount = amount;
}
public String getCustomer() {
return customer;
}
public double getAmount() {
return amount;
}
}
billing/util/Formatter.java (package billing.util, nested one level deeper than billing, because the package name has one more dot):
package billing.util;
public class Formatter {
public static String money(double amount) {
return String.format("₹%,.2f", amount);
}
}
Main.java (default package — it uses the other two, nothing uses it):
import billing.Invoice;
import billing.util.Formatter;
public class Main {
public static void main(String[] args) {
Invoice invoice = new Invoice("Asha", 17600.5);
System.out.println(invoice.getCustomer() + " owes " + Formatter.money(invoice.getAmount()));
}
}
Full program: examples/ex01-packages-and-multifile/.
🔍 Going deeper — real-world package names: Java convention names packages after your organization's reversed domain, lowercase, like
com.example.billing. This course uses short, single-word package names (billing,todoapp) to keep folder paths short while learning; once you work on a real company codebase, follow its existing convention instead.
What happens if the folders don't match
If you compile by listing every file explicitly, javac trusts each file's own package line and places its .class output accordingly — it does not actually check where the .java source sits. But the moment you don't list a file explicitly (a very common case — see Chapter 3), javac has to go looking for it using the package name as a folder path. If the real folder doesn't match, it cannot find the class, even though the file exists somewhere on disk:
$ find . -type f | sort
./Main.java
./billing/Invoice.java
./billing/Formatter.java <-- declares "package billing.util;" but is NOT inside billing/util/
$ javac -d out billing/Invoice.java Main.java
Main.java:2: error: package billing.util does not exist
import billing.util.Formatter;
^
Main.java:7: error: cannot find symbol
System.out.println(Formatter.money(100));
^
symbol: variable Formatter
location: class Main
2 errors
Formatter.java is sitting right there in billing/, one folder away from where it should be — but javac looked for billing/util/Formatter.java (per the import) and found nothing, because the package billing.util; line inside the file does not match where the file actually lives. The fix is always the same: put the file where its package line says it belongs.
Done here? The practice set for Part 7 — including the solutions — is on the part page.