Modifiers in Java
In Java, modifiers are special keywords that define the accessibility and behavior of classes, methods, and variables. They help control code visibility, inheritance rules, immutability, and even thread safety. By mastering modifiers, you can write secure, organized, and maintainable programs.
Types of Modifiers in Java
Java modifiers are divided into two categories:
Access Modifiers — control visibility of classes, methods, and variables.
Non-Access Modifiers — define additional properties like immutability, thread safety, or inheritance rules.
1. Access Modifiers
Access modifiers decide where a class or member can be accessed from.
private → Accessible only within the same class.
default (package-private) → Accessible within the same package (no keyword required).
protected → Accessible within the same package and by subclasses (even in different packages).
public → Accessible from anywhere in the program.
Get Quipoin’s stories in your inbox
Join Medium for free to get updates from this writer.
Note: Top-level classes can only be public or default.
2. Non-Access Modifiers
Non-access modifiers affect how code behaves, not visibility.
static → Belongs to the class, not individual objects (shared by all).
final → Prevents changes (constant variables, non-overridable methods, non-inheritable classes).
abstract → Defines abstract classes/methods that must be implemented by subclasses.
synchronized → Ensures only one thread can access a method/block at a time.
transient → Skips a variable during serialization.
volatile → Ensures a variable is always read/written directly from main memory (for multithreading).
Why Modifiers Matter
Access Modifiers → Control encapsulation and data security.
Non-Access Modifiers → Control inheritance, memory use, and concurrency.
Tip: Always use the most restrictive access level possible to keep your code safe and maintainable.
Want to master these step by step? Check out my Java in 30 Days Playlist





