Access modifiers in Java control which code may see and use a class, field, method or constructor. Java has four access levels: public (visible everywhere), protected (same package and subclasses), default or package-private (same package only, no keyword) and private (the declaring class only). Choosing the narrowest level that works is the basis of good encapsulation.
This guide explains each level with a runnable example, shows a table you can use as a reference, and covers the rules for classes and for the module system in modern Java (Java 21 and Java 25 LTS).
Access is checked by the compiler. If your code touches something it is not allowed to see, the program does not compile.
| Modifier | Same class | Same package | Subclass in other package | Any other class |
|---|---|---|---|---|
private | Yes | No | No | No |
| default (no keyword) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
The order from most to least restrictive is private, default, protected, public. A good habit is to start with private and widen access only when another class really needs it.
A private member can be used only inside the class that declares it. Fields are usually private so that outside code cannot put the object into an invalid state.
Because balance is private, the only way to change it is deposit, which validates the input. Helper methods that are not part of the public contract should also be private.
If you write no modifier, the member is package-private: visible to every class in the same package and invisible outside it. It suits helper classes that cooperate inside a package but should not be part of its public API.
A class in package shop can use PriceCalculator. A class in another package cannot even name it. Note that "default" here means the absence of a keyword; there is no default access modifier, and the default keyword used in switch and in interfaces is unrelated.
protected gives access to the same package and also to subclasses in other packages. It is meant for members that a parent class wants to share with its children but not with the world.
In another package, a subclass can use the protected member through this or a reference of its own type, but not through a plain Vehicle reference. Overusing protected ties subclasses to the internal design of the parent, so prefer private fields with protected or public methods.
A public member is visible to every class that can see the class itself. Public methods and constructors form the contract you promise to keep stable.
Public fields (apart from constants such as public static final int MAX = 10) are discouraged, because anyone can change them and you cannot add validation later without breaking callers.
Top-level classes can only be public or package-private. They cannot be private or protected. Nested classes can use all four levels. Constructors follow the same rules as methods, which is how the singleton pattern hides new:
Since Java 9, modules add one more layer. A public class is reachable from another module only if its package is exported in module-info.java. Inside a named module, public does not always mean visible to everyone.
Package-private. When you write no modifier, the member is visible only to classes in the same package.
Both allow access within the same package. Protected also allows subclasses in other packages to use the member, while default does not.
No. A top-level class is either public or package-private. Only nested classes can be private or protected.
Not completely. Reflection can bypass private unless the module system or a security policy blocks it, so never treat private as a security feature for secrets.
Java offers private, default, protected and public access, from narrowest to widest. Make fields private, expose behaviour through a small set of public methods, and use package-private access for helpers that stay inside one package. This habit is what makes encapsulation work in practice.