March 17, 2026By Aarti Choudhary

Java Access Modifiers

Java is one of the most widely used programming languages in the world. When  we build applications in Java, we often need to control how different classes and  variables are accessed. Sometimes we want certain data to be available  everywhere, and sometimes we want to restrict it so that only specific classes can  use it. 

This is where Access Modifiers come into play. 

Access modifiers in Java define the visibility or accessibility of classes, methods,  and variables. They help developers control how data is accessed and protect the  internal structure of a program. Using access modifiers properly also helps in  implementing encapsulation, which is one of the core principles of object oriented programming. 

Java provides four types of access modifiers: 

• Public 

• Private 

• Protected 

• Default (no modifier) 

Let’s understand each of them in detail with practical examples. 


Why Access Modifiers Are Important 

Before learning about each modifier, it is important to understand why we need  them. 

In large applications, multiple classes interact with each other. If every variable or  method is accessible from anywhere, it can create problems such as accidental  modification of data or security issues. Access modifiers allow developers to  restrict access and protect important parts of the program.

They help in: 


• Protecting sensitive data 

• Improving code security 

• Maintaining better structure in programs 

• Implementing encapsulation 

• Controlling access between packages and classes 


1. Public Access Modifier 

The public access modifier provides the highest level of accessibility. When a  class, method, or variable is declared as public, it can be accessed from anywhere  in the program, even from different packages. 

In simple words, if something is public, it is open to the entire application. Example 

public class Student { 


 public String name; 

 public void displayName() { 

 System.out.println("Student Name: " + name); 

 } 

Now we can access this class from another class. 

public class Main { 

 public static void main(String[] args) { 


 Student s = new Student(); 

 s.name = "Aarti";

 s.displayName(); 

 } 

When to Use Public 

Public access is useful when a method or class must be available to the entire  application. 

Examples: 

• Utility classes 

• APIs 

• Common methods used across multiple modules 

However, developers should be careful when using public variables because it  allows anyone to modify the data. 


2. Private Access Modifier 

The private access modifier provides the most restricted access. A private member can only be accessed within the same class. It cannot be accessed  outside the class directly. 

This is commonly used to protect sensitive data. 

Example 

class BankAccount { 

 private double balance = 5000; 

 public void showBalance() { 

 System.out.println("Balance: " + balance); 

 } 

}

Now if another class tries to access the balance directly, it will give an error. public class Main { 


 public static void main(String[] args) { 

 BankAccount account = new BankAccount(); 

 account.showBalance(); 

 } 

In this example, the balance variable is private. This means external classes cannot  change it directly. Instead, we use public methods to access or modify it. 

When to Use Private 

Private is mainly used to: 

• Protect data 

• Implement encapsulation 

• Prevent unwanted access 

Most variables in real-world Java applications are declared private. 


3. Protected Access Modifier 

The protected access modifier is slightly less restrictive than private. Protected  members can be accessed: 

• Within the same class 

• Within the same package 

• By subclasses (child classes), even if they are in different packages, Protected is commonly used when implementing inheritance

Example

class Animal { 


 protected void sound() { 

 System.out.println("Animal makes a sound"); 

 } 

Now another class can inherit from Animal. 

class Dog extends Animal { 

 void bark() { 

 sound(); 

 System.out.println("Dog barks"); 

 } 

Main class: 

public class Main { 

 public static void main(String[] args) { 

 Dog d = new Dog(); 

 d.bark(); 

 } 

Here, the sound() method is protected, so it can be accessed by the child class  Dog. 

When to Use Protected 

Protected access is useful when: 

• Using inheritance 

• Sharing functionality between parent and child classes

• Designing frameworks and reusable components 

4. Default Access Modifier 

If we do not specify any access modifier, Java automatically uses the default  access modifier. This means the member can be accessed only within the same  package

It cannot be accessed from outside the package. 

Example 

class Employee { 

 String name = "John"; 

 void display() { 

 System.out.println(name); 

 } 

This class can be accessed by other classes in the same package, but not from a  different package. 

When to Use Default 

Default access is used when: 

• Classes are meant to be used only within a package 

• We want limited visibility 

• Package-level security is required 


Comparison of Access Modifiers



Public Yes 

Protected Yes No 

Default No 

Private Yes No No No 

This table clearly shows how access levels change depending on the modifier. 


Best Practices for Using Access Modifiers 

To write clean and secure Java code, developers follow certain best practices when using access modifiers. 

1. Use private variables whenever possible 

This protects the data from external modification. 


2. Use getters and setters 

Instead of making variables public, use methods to access them. 


3. Avoid unnecessary public variables 

Public data can be modified from anywhere, which may cause bugs. 


4. Use protected when working with inheritance 

This allows subclasses to access important methods. 


5. Keep classes organized within packages 

Default access works well when classes belong to the same module. 


Conclusion 

Access modifiers are an essential part of Java programming. They help control the visibility of classes, methods, and variables in an application. By using access 

Modifiers correctly, developers can protect data, improve code security, and maintain a clean program structure. 

Java provides four types of access modifiers: public, private, protected, and default. Each modifier has a different level of accessibility and should be used  according to the requirements of the program. 

Understanding access modifiers is also important for interviews and real-world software development. When used properly, they help developers write more secure, maintainable, and well-structured Java applications.


Do visit our channel to know more: SevenMentor

Author:-

Aarti Choudhary


Aarti Choudhary

Expert trainer and consultant at SevenMentor with years of industry experience. Passionate about sharing knowledge and empowering the next generation of tech leaders.

#Technology#Education#Career Guidance
Java Access Modifiers (With Exmaple)