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++) |
