Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Are Access Modifiers in Java: public, private, protected, default?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 28, 2026
6 min read

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).

Table of Contents

  • The four access levels at a glance
  • Using private for hidden details
  • Default access and the package boundary
  • Using protected for subclasses
  • Using public for the API
  • Modifiers on classes, constructors and modules
  • Frequently Asked Questions
    • What is the default access modifier in Java?
    • What is the difference between protected and default access?
    • Can a top-level class be private?
    • Does private stop reflection from reading a field?
  • Summary
  • Related Articles

The four access levels at a glance

Access is checked by the compiler. If your code touches something it is not allowed to see, the program does not compile.

ModifierSame classSame packageSubclass in other packageAny other class
privateYesNoNoNo
default (no keyword)YesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes

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.

Using private for hidden details

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.

java
java
text

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.

Default access and the package boundary

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.

java

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.

Using protected for subclasses

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.

java
java

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.

Using public for the API

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.

java

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.

Modifiers on classes, constructors and modules

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:

java

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.

Frequently Asked Questions

What is the default access modifier in Java?

Package-private. When you write no modifier, the member is visible only to classes in the same package.

What is the difference between protected and default access?

Both allow access within the same package. Protected also allows subclasses in other packages to use the member, while default does not.

Can a top-level class be private?

No. A top-level class is either public or package-private. Only nested classes can be private or protected.

Does private stop reflection from reading a field?

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.

Summary

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.

Related Articles

  • What Is the static Keyword in Java? Variables, Methods, Blocks
  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • What Is Composition vs Inheritance in Java and Which Should You Prefer?
  • What Is the equals and hashCode Contract in Java?
  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • What Is Polymorphism in Java? Compile-Time vs Runtime
  • What Is Inheritance in Java and How Does extends Work?
  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • What Is the static Keyword in Java? Variables, Methods, Blocks