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

ModifierUsed For
finalConstant variables, prevent method overriding, prevent class inheritance
staticShared class members, called without object
abstractAbstract classes/methods, for incomplete class behavior
synchronizedThread safety for methods
volatileShared variable consistency across threads
transientSkip serialization of variables
nativeCall methods implemented in other languages (C/C++)

You may also like...