Access Modifiers in Java (Public, Private, Protected, Default) – Beginner Guide
Access modifiers in Java control who can access your classes, methods, and variables. They are a core part of Java programming and are especially important for beginners to understand object-oriented programming (OOP) and data protection.
In this beginner-friendly guide, you will learn all Java access modifiers — public, private, protected, and default — with simple explanations and practical use cases.
You will also learn about important non-access modifiers in Java such as static, final, abstract, synchronized, volatile, and transient, which control how your Java code behaves in real-world applications.
Types of Access Modifiers in Java
Java provides four access modifiers to control the visibility and accessibility of classes, methods, and variables.
1. public
public allows access from anywhere in the program. It is commonly used when you want classes, methods, or variables to be accessible from other packages and projects.
- Most open permission level
- Accessible from any class and package
- Common in APIs and libraries
2. private
private restricts access to only within the same class. It is mainly used for data hiding and encapsulation in Java.
- Most restrictive access level
- Improves data security
- Used with getters and setters
3. protected
protected allows access within the same package and in subclasses, even if they are in different packages.
- Accessible in same class
- Accessible in same package
- Accessible in subclasses
4. default (package-private)
If no access modifier is specified, Java uses default (package-private) access.
- Accessible only within the same package
- No explicit keyword used
- Useful for internal package logic
Access Modifiers Comparison Table
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
| public | ✓ | ✓ | ✓ | ✓ |
| protected | ✓ | ✓ | ✓ | ✗ |
| default | ✓ | ✓ | ✗ | ✗ |
| private | ✓ | ✗ | ✗ | ✗ |
Non-Access Modifiers in Java
Non-access modifiers do NOT control visibility. Instead, they control the behavior of classes, methods, and variables in Java programs.
1. static
Belongs to the class, not objects
- Access without creating object
- Shared across instances
- Common for utility methods
2. final
Makes elements unchangeable
- Constants
- Prevent method overriding
- Prevent class inheritance
3. abstract
Defines incomplete elements
- Abstract classes
- Abstract methods
- Templates for subclasses
4. synchronized
Thread-safe access
- One thread at a time
- Prevents data inconsistency
- Used in multithreading
5. volatile
Memory visibility
- Read from main memory
- Shared variables
- Concurrency visibility
6. transient
Controls serialization
- Skip serialization
- Sensitive data
- Temporary fields
Java Access Modifiers – Frequently Asked Questions (FAQ)
What are the 4 access modifiers in Java?
The four access modifiers in Java are public, private, protected, and default (package-private). They control where classes, methods, and variables can be accessed from.
What is the difference between public and private in Java?
public allows access from anywhere, while private restricts access to only within the same class. private is commonly used to protect sensitive data.
What is the default access modifier in Java?
If no access modifier is specified, Java uses default (package-private) access. This means the member is accessible only within the same package.
Are non-access modifiers important for beginners?
Yes. Modifiers like static, final, and abstract are commonly used in real Java applications and are important concepts for beginners preparing for jobs and interviews.
