Java Non-Access Modifiers
Java Non-Access Modifiers
Non-access modifiers in Java provide additional properties or behavior to classes, methods, and variables. They do not control access but affect functionality, memory allocation, or inheritance.
1. final Modifier
Variable → Value cannot be changed after initialization.
Method → Cannot be overridden in subclass.
Class → Cannot be inherited.
2. static Modifier
Belongs to class rather than object.
Shared among all objects.
Can be used with variables, methods, blocks, and nested classes.
3. abstract Modifier
Abstract class → Cannot create objects, can have abstract methods.
Abstract method → Declared without body, must be implemented in subclass.
4. synchronized Modifier
Used in multithreading to make a method thread-safe.
Ensures only one thread executes the method at a time.
5. volatile Modifier
Ensures the variable is always read from main memory in multithreaded programs.
Useful for shared variables.
6. transient Modifier
Prevents serialization of a variable.
Useful when you don’t want sensitive data to be saved.
7. native Modifier
Declares that a method is implemented in native code (C/C++), not Java.
Key Points
| Modifier | Used For |
|---|---|
final | Constant variables, prevent method overriding, prevent class inheritance |
static | Shared class members, called without object |
abstract | Abstract classes/methods, for incomplete class behavior |
synchronized | Thread safety for methods |
volatile | Shared variable consistency across threads |
transient | Skip serialization of variables |
native | Call methods implemented in other languages (C/C++) |
