Java Default Access Modifier: Package-Private Explained
Learn how Java's default access modifier (package-private) controls visibility within packages, how it differs from private and protected, and where to use it in package design.

Java's default access modifier, also called package-private, controls visibility at the package level. It applies when you declare a class or member without public, private, or protected; the resulting member is accessible from any class in the same package and invisible to classes in other packages. There is no default keyword to write: you simply omit the other access modifiers.
package com.example.warehouse;
public class StockItem {
int quantity; // package-private field
int reserved; // package-private field, used later in InventoryCalculator
void adjustQuantity(int delta) { // package-private method
quantity += delta;
}
}The quantity and reserved fields and the adjustQuantity method are usable by other classes in com.example.warehouse, but a class in com.example.shipping cannot reference them directly. The same rule applies to top-level classes: a class declared without public is package-private and can be used only inside its own package.
Visibility Rules at a Glance
Package-private sits between private and protected in the access hierarchy. The exact visibility depends on where the accessing code lives.
| Access level | Same class | Same package | Subclass, other package | Any class |
|---|---|---|---|---|
private | Yes | No | No | No |
| default | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
The critical difference from protected is the third column. A subclass in a different package can access a protected member within the rules for protected access, but cannot access a package-private member. This distinction matters when you design class hierarchies that span packages.
Where Package-Private Access Is Useful
Package-private members are a natural fit for internal collaboration within a package. When several classes in the same package cooperate closely, they often need to share state or helper methods that external callers should not see.
package com.example.warehouse;
class InventoryCalculator {
int computeAvailable(StockItem item) {
return item.quantity - item.reserved;
}
}Here InventoryCalculator reads the package-private quantity and reserved fields directly. The class itself is also package-private because it has no public modifier. The fields stay hidden from code outside the package, so the warehouse package can change its internal representation without breaking external consumers.
A similar pattern is common in library design: package-private helpers support package collaborators while the public API remains small.
Comparing Default Access with Private and Protected
Choosing between private, default, and protected depends on how much you want to expose.
private is the right choice when a member is used only by the declaring class itself. It gives you the strongest encapsulation and the most freedom to refactor.
Default access is appropriate when a member is shared among classes in the same package but should not leak outside. This is common for:
- helper methods used by multiple classes in a package
- internal state that collaborating classes read or update
- constructors that should only be invoked from within the package
protected is for members that subclasses in other packages need to access. If you are designing a framework where users extend your classes from their own packages, protected is often necessary. If you are not supporting external subclassing, default access keeps the API tighter.
A useful rule of thumb: start with private, widen to default when a package collaborator needs access, and reach for protected only when cross-package subclassing requires it.
Common Misconceptions
One recurring misconception is that default access means no restriction. It does not. A package-private member is still restricted, just to package scope. Code in another package cannot see it, even if that code is in a subclass.
Another confusion involves the default value of fields. A field declared without an initializer, such as int count;, receives the default value 0. That behavior is unrelated to the access level. The word default appears in both contexts, but they describe different mechanisms.
There is also no explicit default access modifier keyword. Omitting public, private, and protected is what makes a member or top-level class package-private. Interface default methods are a separate feature, not an access modifier.
A third mistake is assuming that package-private members are visible to subclasses in the same package. They are, but that visibility comes from being in the same package, not from the subclass relationship. A subclass in the same package sees the member because of package membership.
Maintainability and Refactoring Impact
Package-private members create implicit coupling within a package. When you rename or change a package-private member, every class in the package that references it must be updated. The compiler enforces this, so the breakage is visible at build time, but the blast radius is still the entire package.
Moving a class to a different package can break access to package-private members. The compiler will report the errors, but the fix may require widening access to protected or public, which changes the API surface. Before moving a class, check which package-private members it relies on and whether the target package can access them.
Package-private access also affects testing. Test classes in the same package can access package-private members directly, which is convenient for unit tests that need to inspect internal state. Tests in a different package cannot, which forces you to either widen access or test through the public API. Many teams keep test classes in the same package specifically to leverage this.
Package Design and Encapsulation Boundaries
The default access modifier is the primary tool for package-level encapsulation. A well-designed package exposes a small public API and hides the rest. Package-private members let collaborating classes share implementation details without making them part of the public contract.
package com.example.warehouse;
import java.util.ArrayList;
import java.util.List;
public class Warehouse {
private final List<StockItem> items = new ArrayList<>();
void registerItem(StockItem item) { // package-private
items.add(item);
}
}The registerItem method is callable from other classes in the warehouse package, such as an importer or a validator, but it is not part of the public API. External code must use the public methods of Warehouse instead.
This boundary is especially valuable in libraries. Public API is a long-term commitment; package-private members are not. You can change, remove, or rename them in any release without breaking downstream users. Keeping internal helpers package-private rather than public reduces the maintenance burden of your library.
When you design a package, decide which classes are part of the public contract and which are internal collaborators. Mark the public contract with public and the internal collaborators with default access. This makes the package's boundaries explicit and keeps the API surface minimal.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.