Sub topic 3
Naming Java Packages
This chapter assumes you can write a basic Java class with a main method, understand classes and methods, and use a command line or IDE to compile Java code.
A package name identifies the namespace that contains a class. A namespace is a named area that prevents unrelated classes from accidentally using the same name. For example, two libraries can both define a class named Parser if their full package names differ.
Use the reverse domain name convention
Java package names usually begin with the reversed domain name of the organization that owns the code:
com.example.shop
org.acme.reporting
net.example.weatherThe domain is reversed because domains are globally unique from right to left. A company that owns example.com can use package names beginning with com.example. This convention lowers the chance that two libraries will choose the same package name.
The domain does not need to be an active website. It represents an ownership namespace. For a company called Acme with the domain acme.org, suitable packages might be:
org.acme.billing
org.acme.billing.invoice
org.acme.inventoryUse lowercase letters for package segments. Package names are case-sensitive, so org.acme.billing and org.acme.Billing are different names. Lowercase names also follow the standard Java convention and avoid confusion with class names, which normally use UpperCamelCase.
Choose stable, meaningful segments
After the organization name, add names that describe the application or subsystem:
| Package name | Meaning |
|---|---|
| `com.example.shop.model` | Classes representing shop data |
| `com.example.shop.service` | Classes containing application operations |
| `com.example.shop.web` | Classes handling web requests |
Each segment must be a valid Java identifier. It cannot contain spaces or hyphens, and it cannot be a Java keyword such as class or package. If a product name contains a hyphen, replace it with a valid form, such as myshop or my_shop. Lowercase letters are preferred.
package com.example.shop.order;
public class Order {
private final long id;
public Order(long id) {
this.id = id;
}
public long getId() {
return id;
}
}The package name is part of the class's identity. The fully qualified name of this class is com.example.shop.order.Order, not simply Order.
Avoid names owned by the Java platform
Do not create application packages beginning with names such as java, javax, sun, or com.sun. Those names are associated with the Java platform or its implementations. Use a namespace controlled by your organization instead.
For personal projects, use a domain or organization name that you control. Tutorial examples often use com.example, but example.com is reserved for documentation and should not identify a real production library.
Package by responsibility
Package names should help readers locate related code. A small application might use:
com.example.shop
com.example.shop.model
com.example.shop.service
com.example.shop.persistenceKeep names stable and avoid adding temporary details such as v2, new, or final. If a version must be exposed, make that a deliberate API decision rather than an accidental folder name.
Package naming also affects access control. A class without public access is available only inside its package, so moving a class to a different package can break code even when its class name stays the same.
Exercise
Your organization owns the domain brightworks.org and is building an invoicing application. What package name would you choose for classes that represent invoices?
Show answer
org.brightworks.invoicing.model is a suitable choice. It uses the reversed domain, lowercase segments, and a meaningful responsibility name.