Sub topic 2
Declaring a Package
Assume you can write and run a basic Java class, understand classes and methods, and use a command line or IDE. This chapter focuses on declaring a package and making the source layout agree with that declaration.
Why packages exist
A package is a named group of related Java types. It provides:
- A namespace, so different libraries can have classes with the same simple name.
- A directory-like structure for organizing source and compiled files.
- An access boundary for package-private members.
For example, two libraries can each define a class named Product. Their full names remain different:
com.example.shop.Productorg.example.catalog.Product
A class's full name is its fully qualified name. It consists of the package name, a dot, and the class name.
The package declaration
A package declaration tells Java which package owns the types declared in the file. It normally appears at the top of the source file, before imports and type declarations.
For a public class, use a matching file name:
src/com/example/shop/model/Product.java
package com.example.shop.model;
public class Product {
private final String name;
private final double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}The declaration does not create the directories. You create them in the source tree, or your build tool creates the required output directories during compilation.
A package declaration must be the first kind of Java statement in the file. A comment may appear before it, but an import or class declaration may not.
Naming and directory conventions
Java package names conventionally use lowercase words. Most projects use a reverse domain name followed by a project or feature name:
| Domain | Typical package prefix |
|---|---|
| `example.com` | `com.example` |
| `acme.org` | `org.acme` |
| `school.edu` | `edu.school` |
A package might therefore be named com.example.shop.model. Its usual source path mirrors the name:
src/
└── com/
└── example/
└── shop/
└── model/
└── Product.javaEach dot in the package name corresponds to a directory level. The Java compiler can sometimes compile files from other locations, but matching the convention prevents confusing classpath and build errors.
Using the declared package
Code in another package can refer to the class by its fully qualified name:
package com.example.shop;
public class Main {
public static void main(String[] args) {
com.example.shop.model.Product product =
new com.example.shop.model.Product("Pen", 1.50);
System.out.println(product.getName());
}
}More commonly, an import lets you use the simple name:
package com.example.shop;
import com.example.shop.model.Product;
public class Main {
public static void main(String[] args) {
Product product = new Product("Pen", 1.50);
System.out.println(product.getName());
}
}An import does not copy or move a class. It only allows the compiler to resolve Product to com.example.shop.model.Product.
Common errors
- Writing
packageafter animport. - Using uppercase letters or spaces in a package name.
- Putting
Product.javain a directory that does not matchcom.example.shop.model. - Forgetting that a public class name must match its file name.
- Assuming two classes with the same simple name are interchangeable.
Exercise
What package declaration belongs at the top of src/org/acme/billing/Invoice.java if the class should have the fully qualified name org.acme.billing.Invoice?
Show answer
Use package org.acme.billing;. The directory path mirrors the package name, and the class's fully qualified name is org.acme.billing.Invoice.