modifier
Modifiers in Java are special keywords that determine the accessibility and behavior of classes, methods, and variables within your codebase. By applying modifiers, you can specify which parts of your program are accessible from other classes or packages, and you can control important characteristics such as immutability, inheritance, and thread safety. Understanding how to use modifiers effectively is essential for writing secure, organized, and maintainable Java applications.
Types of Modifiers in Java
Java modifiers are categorized into two main types:
1. Access Modifiers
2. Non-Access Modifiers
Java modifiers are categorized into two main types:
1. Access Modifiers
2. Non-Access Modifiers
1. Access Modifiers
Access modifiers define the visibility and accessibility of classes, interfaces, methods, and variables. They help you encapsulate your code and protect data from unwanted access.
Types of Access Modifiers
Access modifiers define the visibility and accessibility of classes, interfaces, methods, and variables. They help you encapsulate your code and protect data from unwanted access.
Types of Access Modifiers
Modifier | Class | Package | Subclass ( Outside package ) | Everywhere |
---|---|---|---|---|
private | Yes | No | No | No |
default | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
Explanation
private
default (no modifier, also called "package-private")
protected
public
private
- Class: Members marked private are accessible only within the class where they are declared.
- Package: Not accessible to other classes in the same package.
- Subclass (outside package): Not accessible, even if inherited.
- Everywhere: Not accessible outside the defining class.
- This is the most restrictive modifier, used for encapsulation and hiding sensitive data or implementation details.
default (no modifier, also called "package-private")
- Class: Accessible within the class.
- Package: Accessible to all other classes in the same package.
- Subclass (outside package): Not accessible, even if the class is extended outside the package.
- Everywhere: Not accessible outside the package.
- This is the default level when no access modifier is specified. It is useful for grouping related classes and restricting access to within a package.
protected
- Class: Accessible within the class.
- Package: Accessible to all classes in the same package.
- Subclass (outside package): Accessible to subclasses, even if they are in different packages.
- Everywhere: Not accessible to non-subclass classes outside the package.
- This modifier is commonly used for inheritance, allowing subclasses to access superclass members while still restricting access from unrelated classes.
public
- Class: Accessible within the class.
- Package: Accessible to all classes in the same package.
- Subclass (outside package): Accessible to all subclasses, regardless of package.
- Everywhere: Accessible from any other class in any package.
- This is the least restrictive modifier, used when you want your class or member to be universally accessible.
1.1 Default (Package-Private) Access Modifier
- How to use: No explicit keyword.
- Scope: Accessible only within the same package.
- Example:
class Demo { void show() { System.out.println("Accessible within the same package!"); }}
1.2 Public Access Modifier
- How to use: public
- Scope: Accessible from anywhere in the program (any package).
- Example:
package quipohouse;public class HelloWorld { public void message() { System.out.println("Welcome to Quipo House"); }}// In another file, same package:HelloWorld obj = new HelloWorld();obj.message();
1.3 Private Access Modifier
- How to use: private
- Scope: Accessible only within the same class.
- Example:
class Message { private String msg; public String getMessage() { return this.msg; } public void setMessage(String msg) { this.msg = msg; }}// Accessing private variable via getter/setterMessage m = new Message();m.setMessage("Welcome To Quipo House");System.out.println(m.getMessage());
Note: Top-level classes cannot be private.
1.4 Protected Access Modifier
- How to use: protected
- Scope: Accessible within the same package and in subclasses (even outside the package).
- Example:
public class HelloWorld { protected void message() { System.out.println("Welcome to Quipo House"); }}public class Main extends HelloWorld { public static void main(String[] args) { HelloWorld obj = new HelloWorld(); obj.message(); // Accessible because Main is a subclass }}
Note: Top-level classes cannot be protected.
Note: Top-level classes can only be public or default (package-private). They cannot be private or protected.
2. Non-Access Modifiers
Non-access modifiers do not affect visibility but instead control other aspects of code behavior, such as memory management, inheritance, and concurrency.
Common Non-Access Modifiers
Non-access modifiers do not affect visibility but instead control other aspects of code behavior, such as memory management, inheritance, and concurrency.
Common Non-Access Modifiers
Modifier | Use / Effect |
---|---|
static | Belongs to the class, not to instances. Shared among all objects. |
final | Prevents inheritance (class), method overriding, or variable reassignment. |
abstract | Declares a class that cannot be instantiated or a method that must be implemented. |
synchronized | Restricts access to a method/block to one thread at a time (thread safety). |
transient | Prevents variable from being serialized. |
volatile | Variable value is always read from main memory, not thread cache (used in multithreading). |
Explanations and Examples
static
static
- Used for fields and methods that belong to the class rather than any object.
- Example:
static int count = 0; // Shared by all instances of the class
final
- Used to declare constants, prevent method overriding, or prevent inheritance.
- Example:
final int MAX = 100; // Cannot be changed after assignment
abstract
- Used for classes that cannot be instantiated and methods that must be implemented by subclasses.
- Example:
abstract class Animal { abstract void makeSound();}
synchronized
- Ensures that only one thread can execute a method or block at a time.
- Example:
synchronized void increment() { // Only one thread can execute this at a time}
transient
- Marks a variable to be skipped during serialization.
- Example:
transient int tempData; // Not saved during serialization
volatile
- Ensures that the value of a variable is always read from main memory, not from a thread's local cache.
- Example:
volatile boolean flag; // Always read from main memory
Modifiers in Java are essential for writing secure, maintainable, and efficient code.
Mastering modifiers will help you enforce encapsulation, protect your data, and design robust Java applications.
- Access modifiers (public, private, protected, and default) control where your classes and members can be accessed.
- Non-access modifiers (static, final, abstract, synchronized, transient, volatile) define how your code behaves in terms of memory, inheritance, and concurrency.
Mastering modifiers will help you enforce encapsulation, protect your data, and design robust Java applications.
Tips: Always choose the most restrictive access level that makes sense for your code. This helps prevent accidental misuse and keeps your codebase secure and organized.