In Java, modifiers are keywords that define the access scope, behavior, or functionality of classes, methods, constructors, or variables. Modifiers help developers structure programs effectively, enhance security, and control access levels. They are divided into two main categories: access modifiers and non-access modifiers.
Types of Modifiers in Java
- Access Modifiers
- Control the visibility of classes, methods, and variables.
- Non-Access Modifiers
- Add functionality to classes, methods, or variables.
1. Access Modifiers in Java
Access modifiers control how accessible a class, method, or variable is to other parts of the program. There are four types:
Modifier | Class | Package | Subclass | World (Other Packages) |
---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
protected | No | Yes | Yes | No |
default | No | Yes | No | No |
private | No | No | No | No |
1.1 public Modifier
The public modifier makes a member accessible from anywhere in the program.
Example:
class PublicExample {
public String message = "Accessible from anywhere!";
public void displayMessage() {
System.out.println(message);
}
}
public class Main {
public static void main(String[] args) {
PublicExample example = new PublicExample();
example.displayMessage();
}
}
Output:
Accessible from anywhere!
1.2 private Modifier
The private modifier restricts access to within the class only.
Example:
class PrivateExample {
private String secret = "This is private";
private void displaySecret() {
System.out.println(secret);
}
public void show() {
displaySecret(); // Accessible within the class
}
}
public class Main {
public static void main(String[] args) {
PrivateExample example = new PrivateExample();
example.show();
// example.secret or example.displaySecret() cannot be accessed here
}
}
Output:
This is private
1.3 protected Modifier
The protected modifier allows access within the same package and to subclasses in other packages.
Example:
class ProtectedExample {
protected String info = "Protected Info";
protected void displayInfo() {
System.out.println(info);
}
}
class SubClass extends ProtectedExample {
public void showInfo() {
displayInfo(); // Accessible in subclass
}
}
public class Main {
public static void main(String[] args) {
SubClass obj = new SubClass();
obj.showInfo();
}
}
Output:
Protected Info
1.4 Default Modifier
The default modifier (no keyword) restricts access to the same package only.
Example:
class DefaultExample {
String message = "Default access";
void displayMessage() {
System.out.println(message);
}
}
public class Main {
public static void main(String[] args) {
DefaultExample example = new DefaultExample();
example.displayMessage();
}
}
Output:
Default access
2. Non-Access Modifiers in Java
Non-access modifiers provide additional functionality or properties to Java classes, methods, or variables.
Types of Non-Access Modifiers
- static
- final
- abstract
- synchronized
- transient
- volatile
2.1 static Modifier
The static modifier allows a member to belong to the class rather than an instance. It can be accessed without creating an object.
Example:
class StaticExample {
static int count = 0;
StaticExample() {
count++;
}
static void displayCount() {
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
new StaticExample();
new StaticExample();
StaticExample.displayCount(); // Access static method directly
}
}
Output:
Count: 2
2.2 final Modifier
The final modifier prevents modification. It can be applied to variables, methods and classes.
- Final Variable: Value cannot be changed.
- Final Method: Cannot be overridden.
- Final Class: Cannot be subclassed.
Example:
final class FinalClass {
final int MAX_VALUE = 100;
final void displayValue() {
System.out.println("Max Value: " + MAX_VALUE);
}
}
2.3 abstract Modifier
The abstract modifier defines a class or method that must be implemented by subclasses.
Example:
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
dog.sleep();
}
}
Output:
Bark
Sleeping…
2.4 synchronized Modifier
The synchronized modifier ensures that only one thread can access a method at a time.
Example:
class SyncExample {
synchronized void printNumbers() {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class Main {
public static void main(String[] args) {
SyncExample example = new SyncExample();
Thread t1 = new Thread(example::printNumbers);
Thread t2 = new Thread(example::printNumbers);
t1.start();
t2.start();
}
}