What are Access Modifiers?
Access modifiers are keywords in Java that define the accessibility of classes, methods, constructors and variables.
They help us control where a particular member can be accessed.
Types of Access Modifiers
Java provides four access modifiers:
public
private
protected
default
1) Public
The public modifier allows a member to be accessed from anywhere, including other packages.
public class Student {
public String name = "John" ;
public void display () { System . out . println ( name ); } }
Enter fullscreen mode
Exit fullscreen mode
Here, the name variable and display() method can be accessed from anywhere.
2) Private
The private modifier allows a member to be accessed only within the same class.
public class Student {
private int age = 22 ;
private void display () { System . out .…