// Access and Non-Access Modifiers in Java //
Modifiers are the keywords in java that are used to change the meaning of
variables or methods.
they are catagorized in two parts >
1. Access Control modifier
2. Non Access Modifier
Access Control Modifier : there are four Access control modifier that can
access the level of class, variable, methods and constructor.
a. default(not mentioned) : has scope only inside the
same package
b. public : can be accessible anywhere
c. private : scope within the same class
d. protected : has scope within same package and all sub classes
Non Access Modifier >
Java provides a number of non-access modifiers to achieve many other
functionality.
- The static : modifier for creating class, methods and variables.
eg: static int a=5;
static void fun(){}
static class Hello{} // inner class
- The final modifier for finalizing the implementations of classes, methods,
and variables.
eg final int a=10; // value can not be changed once initialized
with method (method cannot be override)
with class (class cannot be extend)
- The abstract modifier for creating abstract classes and methods.
abstract class MyClass{} // can not be instantiated
abstract void myFun();
- The synchronized and volatile modifiers, which are used for threads.
The synchronized keyword used to indicate
` that a method can be accessed by only one thread at a time.
synchronized void myMethod(){}
The volatile modifier is used to let the JVM know that a thread
accessing the variable must always merge its own private copy of
the variable with the master copy in the memory.