Access modifiers in Java are keywords that set the visibility of a class, method, or variable. Java provides exactly four: public, private, protected, and default (no keyword). Each one controls which classes or packages can read or change a member, making them the foundation of encapsulation and secure object-oriented design.
- Key Takeaway 1: Java has exactly four access levels. Using the wrong one is one of the most common beginner mistakes in object-oriented code.
- Key Takeaway 2:
privateis the safest default for fields. Start there and open up only when you have a reason. - Key Takeaway 3: The default modifier (no keyword) restricts access to the same package, which surprises many students who assume it means accessible everywhere.
- Key Takeaway 4:
protectedmatters most in inheritance scenarios. It lets subclasses in other packages reach a member while keeping it hidden from unrelated code. - Key Takeaway 5: According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, meaning solid Java fundamentals still carry real career weight.
Types of Access Modifiers in Java
Access modifiers in Java enforce encapsulation, one of the four pillars of object-oriented programming. Encapsulation means bundling data and the methods that operate on it together, then deciding deliberately who gets to touch what. Without access control, any class anywhere in your project could read or overwrite a field, making bugs nearly impossible to trace.
Think of it like a hospital records system. A patient’s file should be readable by the treating doctor, partially visible to the billing department, and completely invisible to a random visitor. Access specifiers in Java let you model exactly that kind of rule in code.
This is also where the principle of least privilege enters software development. Give each piece of code only the access it genuinely needs, nothing more. Security engineers and ethical hackers flag overly permissive access as a vulnerability in code reviews, so developing this habit early pays off if you ever move toward secure development or a course like the CEH v13 AI certification.
If you are just beginning to build your toolkit, it also helps to know which software development tools to learn first before going deep on any one language feature.
What Access Modifiers Do
public: Accessible Everywhere
A public class, method, or field is visible to every other class in every package. This is the most open modifier. The public access specifier in Java is what you use for APIs, utility methods, and anything that genuinely needs to be called from outside your package.
A classic example is System.out.println(). It is public because the entire Java ecosystem needs to call it. In your own code, make a method public only when it forms part of a deliberate interface to the outside world, not just because it is convenient.
private: Restricted to the Declaring Class
The private access modifier in Java means only the class that declares the member can use it. No subclass, no other class in the same package, nobody. This is the tightest restriction available and the right default for most fields.
A practical example: if you have a BankAccount class with a balance field, making it private forces all changes to go through a deposit() or withdraw() method where you can validate the input. Without private, any class could set balance = -99999 directly. That is not a hypothetical risk; it is the kind of bug that shows up in real codebases and creates security holes.
You will also hear the term java private class. Top-level classes cannot be private in Java (see the FAQs section below for detail), but inner classes can be, which is a useful pattern for hiding implementation details.
protected: For Inheritance and Same-Package Access
The protected access modifier in Java gives access to the declaring class, all classes in the same package, and any subclass in any package. It exists specifically to support inheritance hierarchies.
Say you are building a Vehicle base class with a fuelLevel field. You want Car and Truck subclasses to read it directly, but you do not want a completely unrelated WeatherApp class touching it. Marking fuelLevel as protected achieves exactly that. The protected access specifier in Java is the middle ground between public and private when inheritance is involved.
default (Package-Private): No Keyword Required
If you write a class, method, or field with no access modifier at all, Java applies the default access modifier, sometimes called package-private. It is visible to all classes in the same package but invisible to classes outside it.
This trips up a lot of BTech and BCA students. They assume no modifier means open to all, but it is actually more restrictive than protected in one key way: a subclass in a different package cannot access a default member, even through inheritance. The Oracle Java documentation spells this out clearly in its access control table.
Understanding this well helps when you study Java exceptions and common runtime errors, because access violations at runtime often trace back to misunderstood package boundaries.
Visibility Table: Java Access Modifiers at a Glance
The table below maps each modifier to where it is accessible. This is the single most useful reference when you are deciding which modifier to pick.
| Modifier | Same Class | Same Package | Subclass (Different Package) | Other Classes (Different Package) |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes |
| protected | Yes | Yes | Yes | No |
| default (no keyword) | Yes | Yes | No | No |
| private | Yes | No | No | No |
Source: Oracle Java Tutorials, Access Control.
A Quick Access Modifiers in Java Example
Here is a minimal illustration. Imagine a class Student in package com.university:
public String name— any class anywhere can read the name.private int marks— onlyStudentitself can read or change marks.protected String department—Student, other classes incom.university, and any subclass likePGStudentcan access it.String rollNumber(no modifier) — only classes insidecom.universitycan see it.
Running a Java program that tries to access marks from a different class will throw a compile-time error, not a runtime exception. That is by design: the compiler catches the violation before the code ever runs.
Choosing Modifiers Well
The most common real-world mistake is defaulting to public for everything because it just works. It does work, but it destroys encapsulation. In a large codebase, a single carelessly public field can be modified by dozens of unrelated classes, making refactoring a nightmare six months later.
A practical rule used in professional Java teams: start every field as private. Expose it through a getter or setter only if something outside the class genuinely needs it. Promote to protected only when you are building an inheritance hierarchy on purpose. Use public for the deliberate, stable API surface of your class.
Access Protection in Java and Security
This matters beyond just clean code. According to the OWASP Top 10 (2021 edition), broken access control is the single most critical web application security risk, ranking first out of ten categories. That is at the application level, but the same thinking applies at the code level. Developers who understand access protection in Java write tighter, harder-to-exploit code from the start.
If you are interested in how these principles extend to version control and repository security, the article on GitHub and GitLab security features is worth reading alongside this one.
Access Modifiers in Java for Career Contexts
Access modifiers come up in almost every Java interview. According to the TIOBE Index (April 2025), Java ranks in the top three programming languages by popularity. In India specifically, Java is among the most in-demand skills for software roles in Bengaluru, Hyderabad, and Pune, with entry-level Java developer salaries ranging from Rs 3.5 to Rs 6 lakh per annum according to Naukri.com salary data. Interviewers at Indian IT companies, product startups, and MNCs test this topic directly. Students preparing for placements after a BSc Computer Science or checking jobs and salary after MCA should treat access modifiers as a non-negotiable fundamental.
Whether you are aiming to be a full stack developer or a software developer, access control is one of those topics that separates candidates who understand object-oriented design from those who just memorised syntax.
Frequently Asked Questions
What are access modifiers in Java?
Access modifiers in Java are keywords (public, private, protected, and default) that define the visibility and accessibility of classes, methods, and fields. They enforce encapsulation by restricting which parts of a program can interact with a given piece of code. Choosing the right modifier is a core part of writing secure, maintainable Java.
What is the default access modifier in Java?
The default access modifier, also called package-private, applies when you write no modifier keyword at all. It makes the member accessible to all classes within the same package but invisible to classes in other packages, including subclasses defined outside the package. Many beginners confuse it with public, but it is considerably more restrictive.
What is the difference between public and private in Java?
public makes a member accessible from any class in any package, with no restrictions. private restricts access to only the class that declares it, blocking even subclasses and same-package classes. In practice, fields should almost always be private, while methods that form a class’s external interface are marked public.
What does protected mean in Java?
The protected access modifier allows access from the declaring class, all classes in the same package, and any subclass in any package. It is designed for inheritance: you use it when a parent class needs to share a member with its children but wants to keep it hidden from unrelated classes outside the package.
Can a class be private in Java?
A top-level class in Java cannot be declared private or protected. Top-level classes can only be public or default (package-private). However, inner classes and nested classes can be private, which is a useful pattern for hiding helper logic that should never be exposed outside the enclosing class.
Which access modifier should I use by default in Java?
Start with private for all fields and expose them through getters or setters only when another class genuinely needs access. Use protected when building an inheritance hierarchy, and reserve public for the stable, intentional API surface of your class. This approach follows the principle of least privilege and is standard practice in professional Java development.
You now have a complete picture of access modifiers in Java: what each one does, when to use it, and why it matters for both clean code and security. To put this into practice this week, try these three steps:
- Go back to any Java project you have written and audit every field. Change any
publicfield toprivateand add getters or setters only where another class genuinely needs access. - Write a small two-package project with a parent and child class to observe exactly what
protectedand default members do across package boundaries. Seeing the compiler error firsthand makes it stick. - Explore the broader 3.0 University online certification courses catalogue. Strong Java fundamentals feed directly into ethical hacking, secure coding, AI development, and blockchain work. If you are serious about cybersecurity, the CEH v13 AI course builds on exactly the kind of access-control thinking you have just practised here.
Last updated: May 2025. Reviewed by the 3University editorial team.