Access Modifiers in Java
Access modifiers control the visibility (accessibility) of classes, methods, and variables in Java. They determine which parts of your code can be accessed from other parts.
There are two main categories: Access Modifiers (control visibility) and Non-Access Modifiers (control behavior).
Access Modifiers
Control the visibility and accessibility of classes, methods, and variables.
1. public
Accessible from anywhere, in any class or package.
Key Characteristics:
- Most open permission level
- Can be accessed from any other class
- No restrictions on accessibility
2. private
Accessible only within the same class.
Key Characteristics:
- Most restrictive access level
- Used to hide data (encapsulation)
- Cannot be accessed from outside the class
3. protected
Accessible in specific contexts:
Accessible:
- Within the same class
- Within the same package
- In subclasses (even in different packages)
4. default (no keyword)
When no access modifier is specified.
Key Characteristics:
- Accessible only within the same package
- Also called package-private
- No explicit keyword needed
Access Modifiers Comparison
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
| public | ✓ | ✓ | ✓ | ✓ |
| protected | ✓ | ✓ | ✓ | ✗ |
| default | ✓ | ✓ | ✗ | ✗ |
| private | ✓ | ✗ | ✗ | ✗ |
Non-Access Modifiers
These modifiers do NOT control visibility, but change the behavior of classes, methods, and variables.
1. static
Belongs to the class, not objects.
Key Characteristics:
- Can be accessed without creating an object
- Shared across all instances of the class
- Memory efficient for common data
2. final
Makes elements unchangeable.
Usage:
- Variable: value cannot be changed (constant)
- Method: cannot be overridden
- Class: cannot be inherited
3. abstract
Defines incomplete elements.
Usage:
- Class: cannot be instantiated
- Method: has no body; must be implemented by subclasses
- Used for creating templates and contracts
4. synchronized
Used in multithreading.
Key Characteristics:
- Method can be accessed by only one thread at a time
- Prevents data inconsistency in concurrent programs
- Ensures thread safety
5. volatile
Used in concurrency.
Key Characteristics:
- Tells the JVM to always read the variable from main memory, not cache
- Ensures visibility of changes across threads
- Used when variables are shared between threads
6. transient
Controls serialization.
Key Characteristics:
- Variable will not be serialized
- Used when you don't want to save sensitive data
- Value becomes default after deserialization
Non-Access Modifiers Summary
| Modifier | Purpose | Common Use Case |
|---|---|---|
| static | Belongs to class, not objects | Utility methods, constants |
| final | Makes elements unchangeable | Constants, prevent override/inheritance |
| abstract | Defines incomplete elements | Templates, contracts for subclasses |
| synchronized | Thread-safe access | Multithreading, concurrent programs |
| volatile | Memory visibility | Shared variables in concurrency |
| transient | Skip serialization | Sensitive data, temporary fields |
