Java Access Modifiers

Yashod Perera
3 min readFeb 21, 2023

This article is to understand how to and where to apply access modifiers in Java. We have following 4 access modifiers.

  1. public — anybody can access
  2. private — can only use inside the class
  3. default — can only use inside the package
  4. protected — can only use inside the package and in child classes
Photo by Jonathon Young on Unsplash

Why we need access modifiers?

We are using variables which are okay to accessible from anyplace, some needed to access within the package or within the class. Hence we need to mention what are the accessing limitation for those variables, methods and even classes hence we use access modifiers.

public access modifier

If you need to access the variable, method or class from anyplace you have to make it public as follow.

public class Student {
public String name;

public Student(String name) {
this.name = name;
}

public String getName() {
return this.name;
}
}

In the above example we create a Student class which is publicly accessible from any package, any class as follows.

public class StudentRegistration{
public static void main(String[] args) {
Student sam = new Student("Sam"); // Can access Student class
System.out.println(sam.getName()); // Since the getName is public we can access it from anywhere
sam.name = "John"; // Can access name since it is public
}
}

But there are cases where we have to restrict some access. As an example we are not going to change the Student name after initiate. Then we need to make those private.

private access modifier

If you need to restrict the access within the class then private access modifier is used.

public class Student {
private String name;

public Student(String name) {
this.name = name;
}

public String getName() {
return this.name;
}
}

In the above example we have changed the name as a private variable.

public class StudentRegistration{
public static void main(String[] args) {
Student sam = new Student("Sam"); // Can access Student class
System.out.println(sam.getName()); // Since the getName is public we can access it from anywhere
sam.name = "John"; // Error since name is not accessible
}
}

Above code is not going to be compiled since sam.name is not accessible since it is private.

private variables are not even accessible for child classes as well.

class Person {
private String name;
}

class Student extends Person{
public String getName() {
return this.name; // this will give a compilation error since the name is private
}
}

default access modifier

If you are not using any access modifier then it become default . These types of variables, methods and classes are only accessible inside the package.

protected access modifier

This access modifier has more access that default access modifier which can accessible by child classes. As an example if Person class is in package a and Student class is in package b and Student class is a child class of Person class. Then if the Person class has a default variable it is not accessible inside the Student class but if the Person class has a protected variable then it is accessible inside the Student class.

Hope you got a basic understanding of Access modifiers in Java. If you have found this helpful please hit that 👏 and share it on social media :)

--

--

Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor