Types of modifiers in java
The visibility of a class, method, constructor, and variable is controlled by access modifiers. Whereas non-access modifiers are used to provide other functionalities like synchronizing a method or block, restricting the serialization of a variable, etc.
Access Modifier
Types of access modifier in java-
Private Access Modifier- Private access modifiers are basically used before method, constructor, and variable. Private access modifiers can not be used with class and interface.
It can be used with the class when the class in the inner class i.e. class inside another class. Private is the most restricted level of access.
Important Points About access Modifier-
Example-
package Java_Test;
//class with private variable class PrivateModifier {
private int a = 10;
public int getA() {
return a;
} public void setA(int a) { this.a = a; } } public class AccessModifiers {
public static void main(String[] args) {
PrivateModifier pm = new PrivateModifier(); System.out.println(pm.getA());
}
}
Default Access Modifier
Default access modifiers are basically used before method, constructor, and variable(If we are not declaring any modifier then it is considered as default access modifier). If we declare any method, constructor or variable as default then it will be accessed from any class within the package.
Protected Access Modifier
Protected access modifiers are basically used before method, constructor, and variable. Protected can not be used with the class. If we declare any method, constructor or variable as protected then it will be accessed from any class within the package and outside the package through child class.
Public Access Modifier
A public access modifier is accessible anywhere.
Non Access Modifier-
There are various non-access modifiers in java. They are used to provide some information to JVM.
Know More:
Follow our page JAVA Object oriented programming Language, where you can find every topic related to java