Sub topic 1
Why Java Has Packages
Java packages group related classes under a named namespace. A namespace is a naming area that prevents unrelated classes from accidentally sharing the same identity.
As a project grows, many teams may create classes with common names such as User, Config, Report, or Service. Packages let each team use these names safely:
com.example.sales.Usercom.example.support.User
These are different classes because their fully qualified names differ. A fully qualified name includes the package name and the class name.
Prerequisites
This chapter assumes that you can write and run a basic Java class with a main method. You should also know basic class, method, and field syntax, and have used either a command line or an IDE to compile Java code.
Packages create namespaces
Without packages, every class would share one global naming area. Two classes could not have the same name, even if they represented unrelated concepts.
Packages solve this problem and also communicate the structure of an application. For example:
com.example.shop.modelcan contain product and order data classes.com.example.shop.servicecan contain business operations.com.example.shop.uican contain user-interface classes.
The package name becomes part of each class's identity.
A package also creates an access boundary. Classes in the same package can cooperate through package-private members, which are members declared without public, protected, or private. This can hide implementation details from code in other packages.
A package declaration identifies a class
A package declaration normally appears as the first statement in a source file, before imports and the class declaration.
package com.example.shop.model;
public class Product {
private final String name;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
}This class's fully qualified name is com.example.shop.model.Product, while Product is its simple name.
Another class can refer to the full name when it needs to identify the class precisely:
public class Main {
public static void main(String[] args) {
com.example.shop.model.Product product =
new com.example.shop.model.Product("Pen");
System.out.println(product.getName());
}
}In everyday code, imports usually make this syntax shorter. The important point is that the package remains part of the class's identity, even when the source uses only the simple name.
Package naming conventions
Java packages conventionally use lowercase names. Project packages commonly begin with a reversed domain name:
com.example.shoporg.example.libraryio.example.analytics
Reversing the domain reduces collisions between organizations. If a company controls example.com, its Java packages can begin with com.example.
Use additional segments to describe the application or subsystem. Avoid vague names such as stuff or misc, because package names should help readers locate and understand code.
Why this matters in practice
Packages provide three related benefits:
| Benefit | What it provides |
|---|---|
| Namespaces | Classes with the same simple name can coexist |
| Organization | Related code has a predictable home |
| Access boundaries | Package-private implementation details stay local |
Packages do not automatically make a design good. A package with hundreds of unrelated classes is still difficult to understand. Use packages to express meaningful boundaries, not merely to create more directories.
Exercise
Why can com.example.sales.User and com.example.support.User both exist in the same Java application?
Show answer
Their fully qualified names are different because their package names are different. The package acts as a namespace, so the simple name User is not ambiguous until code chooses how to refer to each class.