Modifiers in Java
Modifiers in Java are keywords that define the scope, accessibility, and behavior of classes, methods, constructors, and variables. They are broadly classified into two categories:
- Access Modifiers
- Non-Access Modifiers
1. Access Modifiers
Access modifiers control the visibility of classes, methods, constructors, and variables.
Types of Access Modifiers:
Modifier | Class | Package | Subclass | World |
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes | No |
default | Yes | Yes | No | No |
private | Yes | No | No | No |
- public: Allows access from anywhere.
- protected: Allows access within the same package and to subclasses outside the package.
- default (no modifier): Allows access only within the same package.
- private: Allows access only within the same class.
Examples:
public class Test {
private int data = 10; // Accessible only within this class
protected int value = 20; // Accessible within the same package and subclasses
public void display() { // Accessible from anywhere
System.out.println(“Hello, World!”);
}
}
2. Non-Access Modifiers
Non-access modifiers provide functional properties to classes, methods, and variables.
Types of Non-Access Modifiers:
Modifier | Use |
static | Indicates a class-level variable or method (shared by all objects). |
final | Prevents modification (variable value cannot change, methods cannot be overridden, classes cannot be subclassed). |
abstract | Defines a method without implementation or a class with incomplete implementation. |
synchronized | Ensures thread safety in concurrent execution. |
volatile | Indicates a variable can be modified by multiple threads. |
transient | Prevents serialization of a field. |
native | Declares a method implemented in native code (e.g., C/C++). |
strictfp | Enforces strict floating-point calculations. |
Key Non-Access Modifiers:
- static
- Used for variables and methods that belong to the class, not to specific objects.
- Example:
class Counter {
static int count = 0; // Shared across all objects
Counter() {
count++;
}
}
public class Main {
public static void main(String[] args) {
new Counter();
new Counter();
System.out.println(Counter.count); // Output: 2
}
}
- final
- Class: Prevents inheritance (e.g., final class).
- Method: Prevents overriding (e.g., final void).
- Variable: Makes the value immutable (e.g., final int).
Example:
final class A {
final int MAX = 100;
final void display() {
System.out.println(“This is a final method.”);
}
}
- abstract
- Used to define incomplete classes or methods.
- Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println(“Drawing Circle”);
}
}
- synchronized
- Ensures thread-safe execution.
- Example:
synchronized void print() {
System.out.println(“Thread-safe method”);
}
- transient
- Excludes variables from serialization.
- Example:
class Demo implements Serializable {
transient int temp; // Won’t be serialized
}
Usage Summary
Modifiers help you:
- Define who can access your code (Access Modifiers).
- Add specific behavior to variables, methods, or classes (Non-Access Modifiers).
Effective use of modifiers ensures a clean, secure, and robust code structure.