Access Modifiers in Java – Complete Guide with Examples
When you first start learning Java, one of the first concepts you will encounter is Access Modifiers in Java. They control the visibility, accessibility and scope of classes, variables, methods and constructors. Put simply, access modifiers determine who can access what in your program.
Table Of Content
- 🔑 What are Access Modifiers in Java?
- 📌 Types of Access Modifiers in Java
- Public Access Modifier
- Private Access Modifier
- Protected Access Modifier
- Default Access Modifier (No Keyword)
- 📊 Quick Comparison Table – Types of Access Modifiers in Java
- 🔧 Non Access Modifiers in Java
- 💡 Why Use Access Modifiers in Java?
- 🎯 Real-Life Example of Access Modifiers
- 🤔 Access Modifiers vs Non Access Modifiers in Java
- 📝 Interview Questions on Access Modifiers in Java
- ✅ Conclusion
- Related Reads
Without access modifiers, a Java program could be chaotic. Anyone could access and change anything. It is kind of like locking your house without a key! Access modifiers determine who has access to the objects you create.
🔑 What are Access Modifiers in Java?
Access modifiers in Java are keywords used to specify the accessibility of classes, methods, constructors and variables. They play an important role in encapsulation, that is a one of the key concepts of Object Oriented Programming, because it limits the accessibility of class members.
In Java, there are four main types of access modifiers:
- Public
- Private
- Protected
- Default (no modifier)
📌 Types of Access Modifiers in Java

Let’s break down each type of access modifier with examples and real-life analogies
Public Access Modifier
- Keyword: public
- Scope: Accessible from anywhere in the project.
- Analogy: Think of a public park – anyone can enter.
public class Student {
public String name = "John";
public void showName() {
System.out.println("Name: " + name);
}
}
Here, both name and showName() can be accessed from any class.
Private Access Modifier
- Keyword: private
- Scope: Accessible only within the same class.
- Analogy: Your ATM PIN – only you should know it, not even your best friend.
class BankAccount {
private double balance = 1000;
private void showBalance() {
System.out.println("Balance: " + balance);
}
}
Here, balance and showBalance() are hidden from outside classes.
Protected Access Modifier
- Keyword: protected
- Scope: Accessible within the same package and also by subclasses in different packages.
- Analogy: Think of a family recipe – it’s shared among relatives (subclasses) but not with strangers.
class Vehicle {
protected String type = "Car";
}
class Car extends Vehicle {
public void display() {
System.out.println("Vehicle type: " + type);
}
}
Default Access Modifier (No Keyword)
- Keyword: none (just leave it blank)
- Scope: Accessible only within the same package.
- Analogy: A neighborhood community park – only locals can enter.
class Teacher {
String subject = "Math"; // default access
}
📊 Quick Comparison Table – Types of Access Modifiers in Java
|
Modifier |
Within Class | Same Package | Subclass (diff package) | Other Packages |
|
Public |
✅ | ✅ | ✅ | ✅ |
|
Private |
✅ | ❌ | ❌ |
❌ |
| Protected | ✅ | ✅ | ✅ |
❌ |
| Default | ✅ | ✅ | ❌ |
❌ |
🔧 Non Access Modifiers in Java

In addition to access modifiers, Java has non access modifiers that define other properties of a methods, classes or variables. Non access modifiers do not deal with visibility but control behavior.
Common Non Access Modifiers in Java:
- static – Belongs to the class, not the object.
- final – Used to declare constants or prevent inheritance/overriding.
- abstract – For abstract classes and methods.
- synchronized – Controls thread access.
- volatile – For variables modified by multiple threads.
👉 Example:
class Demo {
static int count = 0; // static variable
final int MAX = 100; // final variable
}
Including non access modifiers in Java makes your program more efficient and structured.
💡 Why Use Access Modifiers in Java?
- Helps in data hiding and security
- Provides encapsulation
- Controls variables and methods scope
- Improves code maintainability
- Helps in reusability and inheritance
🎯 Real-Life Example of Access Modifiers

Think of a company:
- Public: Reception area – anyone can enter.
- Private: CEO’s cabin – only the CEO can access.
- Protected: HR files – employees of the company can access, but outsiders cannot.
- Default: Internal office discussions – limited to staff within the same branch.
🤔 Access Modifiers vs Non Access Modifiers in Java
|
Feature |
Access Modifiers | Non Access Modifiers |
|
Purpose |
Control visibility | Control behavior |
| Examples | public, private, protected, default |
static, final, abstract, synchronized |
| Focus | Security & encapsulation |
Performance & functionality |
📝 Interview Questions on Access Modifiers in Java
- What are the types of access modifiers in Java?
- Difference between default and protected modifier?
- Can we use private constructor in Java?
- Difference between access modifiers and non access modifiers in Java?
- Can abstract methods be private?
✅ Conclusion
In this guide, we discussed Access Modifiers in Java, their type (public, private, protected, default), and non access modifiers in Java using examples, tables, and real-world analogies.
To recap:
- Use access modifiers to control who can access what.
- Use non access modifiers to define how elements behave.
Once you get the hang of these you will have secure, modular and professional Java code.

