Packages
A package is a named namespace for classes. It maps to a directory on disk and controls who can see package-private members. package com.shop.orders; is the first line of the file.
Detailed Explanation
Without packages, every class name would have to be unique in the whole program. Two libraries could not both ship a class called Utils. A package prefixes the name: com.shop.orders.Utils and org.other.Utils are different types.
The package declaration must match the folder path under the source root. com.shop.orders.Order lives in com/shop/orders/Order.java. The compiler and the JVM both use that path to find the class. A mismatch is a compile error or a NoClassDefFoundError later.
import is only a shorthand for the compiler. import java.util.List; lets you write List instead of java.util.List. It does not load code, and it does not make members visible that access rules hide. Wildcard imports (import java.util.*) pull in types, not subpackages. java.lang is imported automatically.
Access and packages are tied together. A member with no modifier is package-private: visible to every class in the same package, hidden from others. That is why splitting a package across two jars is dangerous: they share package-private access if they have the same package name. JPMS modules can hide whole packages from other modules even if types are public.
Convention is reverse-DNS (com.company.product) so names stay globally unique. The unnamed package (no package line) is allowed for tiny examples and forbidden in named modules. Do not ship production code there.
You reach for a new package when a group of types share a reason to change and a boundary you want to enforce. Package by feature (orders, billing) rather than by layer alone (controllers, services) if the alternative would spread one change across four packages. Keep packages small enough that package-private still means "friends", not "the entire application".
The cost of too many packages is import noise and circular dependencies. The cost of one fat package is no encapsulation between features. Cycles (a imports b imports a) compile but freeze the design. Break them by extracting an interface into a third package.
Code Examples
package shop.pricing;
public class Gst {
public static int add(int paise) {
return paise + paise * 18 / 100;
}
}package shop.app;
import shop.pricing.Gst; // shorthand; the JVM still uses the full name
public class Checkout {
public static void main(String[] args) {
System.out.println(Gst.add(10000)); // prints: 11800
System.out.println(shop.pricing.Gst.add(100)); // also legal
}
}Common Gotchas
Package line must match the directory
`package shop.pricing` in `shop/tax/Gst.java` does not compile.
Split packages share package-private access
two jars with `com.shop.util` can see each other's package-private types. Use modules or distinct packages.
`import` does not load the class
first active use loads it. An unused import is a compile warning in most style checks, not a runtime cost.
Wildcard imports can clash
`import java.util.*` and `import java.awt.*` both have a `List`. Write the fully qualified name.