Welcome to The Coding College! In this article, we will explore modifiers in Java. Modifiers are keywords that change the properties or behavior of classes, methods, or variables in Java. They are essential for defining access levels, behavior, and other attributes in a program.
Types of Modifiers
Java modifiers are broadly categorized into two types:
- Access Modifiers
- Non-Access Modifiers
1. Access Modifiers
Access modifiers define the visibility or accessibility of classes, methods, and variables.
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | ✅ | ✅ | ✅ | ✅ |
protected | ✅ | ✅ | ✅ | ❌ |
(default) | ✅ | ✅ | ❌ | ❌ |
private | ✅ | ❌ | ❌ | ❌ |
Examples of Access Modifiers
1. Public
A public
member is accessible everywhere in the program.
public class Main {
public int x = 10;
public void display() {
System.out.println("Public Modifier Example: " + x);
}
}
2. Protected
A protected
member is accessible within the same package and by subclasses.
class Animal {
protected String name = "Lion";
}
public class Main extends Animal {
public static void main(String[] args) {
Main obj = new Main();
System.out.println("Protected Modifier Example: " + obj.name);
}
}
3. Default (Package-Private)
If no modifier is specified, the member is accessible within the same package.
class Main {
int x = 10; // Default access modifier
void display() {
System.out.println("Default Modifier Example: " + x);
}
}
4. Private
A private
member is accessible only within its class.
class Main {
private int x = 10;
private void display() {
System.out.println("Private Modifier Example: " + x);
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}
2. Non-Access Modifiers
Non-access modifiers add additional functionality or specify special properties for classes, methods, or variables.
Categories of Non-Access Modifiers
- Class Modifiers:
final
,abstract
- Method Modifiers:
static
,final
,abstract
,synchronized
- Variable Modifiers:
final
,static
,transient
,volatile
Examples of Non-Access Modifiers
1. Static Modifier
The static
modifier indicates that the member belongs to the class rather than any object.
class Main {
static int count = 0;
static void displayCount() {
System.out.println("Static Modifier Example: " + count);
}
public static void main(String[] args) {
Main.displayCount();
}
}
2. Final Modifier
The final
modifier is used to define constants, prevent method overriding, and inheritance.
- Final Variable Example:
class Main {
final int x = 10;
void display() {
System.out.println("Final Modifier Example: " + x);
}
}
- Final Method Example:
class Parent {
final void display() {
System.out.println("Final Method: Cannot be overridden.");
}
}
class Child extends Parent {
// Error: Cannot override final method
// void display() {}
}
- Final Class Example:
final class Main {
// Cannot inherit this class
}
3. Abstract Modifier
The abstract
modifier is used to declare an abstract class or method.
- Abstract Class Example:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
4. Synchronized Modifier
The synchronized
modifier ensures thread-safe execution of methods or blocks.
class Main {
synchronized void display() {
System.out.println("Synchronized Modifier Example");
}
}
5. Transient Modifier
The transient
modifier prevents a variable from being serialized.
import java.io.Serializable;
class Main implements Serializable {
transient int id; // Not serialized
}
6. Volatile Modifier
The volatile
modifier ensures that changes to a variable are visible to all threads.
class Main {
volatile int flag = 1; // Updated in multithreading
}
Why Use Modifiers?
- Encapsulation: Ensures better control over data.
- Security: Restricts access to sensitive parts of the code.
- Efficiency: Optimizes memory usage and execution.
- Concurrency: Improves thread safety.
Best Practices
- Use
private
for sensitive data to protect it from unauthorized access. - Mark constants as
final static
for clarity and efficiency. - Use
synchronized
carefully to avoid performance bottlenecks. - Opt for
protected
for members that need access in subclasses.
Practice Problems
- Create a
Student
class withprivate
data members and providepublic
getter and setter methods. - Write an example using
final
class to prevent inheritance. - Demonstrate the use of
transient
andvolatile
modifiers in a real-world scenario.
For more coding tips and tutorials, visit The Coding College! 🚀