Apply Now Apply Now Apply Now
header_logo
Post thumbnail
JAVA

Access Modifiers in Java: Types, Examples, and Best Practices

By Vishalini Devarajan

Java access modifiers are crucial in object-oriented programming. They determine how classes, methods, variables, and constructors can be accessed. Understanding access modifiers helps developers create secure, maintainable, and scalable Java applications.

If you are learning Java, access modifiers are key to writing clean code and protecting sensitive data within applications.

In this article, you will learn about access modifiers in Java, the different types available, real-world examples, best practices, common mistakes, and how to use them effectively in Java programs.

Table of contents


  1. TL;DR
  2. Why Are Access Modifiers Important?
  3. Types of Access Modifiers in Java
    • Public
    • Private
    • Protected
    • Default
  4. Public Access Modifier
    • Example of Public Access Modifier
    • When to Use Public Access Modifier
  5. Private Access Modifier
    • Example of Private Access Modifier
    • Why Private Access Matters
  6. Protected Access Modifier
    • Example of Protected Access Modifier
    • When to Use the Protected Access Modifier
  7. Default Access Modifier
    • Example of Default Access Modifier
    • When Default Access Is Useful
  8. Access Modifier Comparison Table
  9. Real World Example of Access Modifiers
  10. Best Practices for Using Access Modifiers
    • Prefer Private for Variables
    • Use Public Only When Necessary
    • Use Protected Carefully
    • Keep Package Utilities Default
    • Follow Encapsulation Principles
  11. Common Mistakes Beginners Make
    • Making All Variables Public
    • Confusing Protected and Default Access
    • Overusing Public Methods
    • Ignoring Encapsulation
  12. Conclusion
  13. FAQs
    • What are access modifiers in Java?
    • How many access modifiers are available in Java?
    • What is the difference between private and protected in Java?
    • Which access modifier is the most secure?
    • Why are access modifiers important in Java?

TL;DR

  1. Access modifiers in Java define the visibility and accessibility of classes, methods, variables, and constructors.
  2. Java provides four access modifiers: public, private, protected, and default.
  3. Using the right access modifier enhances code security, encapsulation, and maintainability.
  4. Private members can only be accessed within the same class, while public members are accessible everywhere.
  5. Protected members allow specific inheritance access, and default access works within the same package.
  6. Access modifiers are widely used in real-world Java applications, APIs, frameworks, and enterprise systems.

What Are Access Modifiers in Java?

Access modifiers in Java are keywords that control the visibility and accessibility of classes, methods, variables, and constructors. They determine which parts of a program can access specific class members, helping developers enforce encapsulation and protect data from unintended access. Java provides four access levels: public, protected, default (package-private), and private, each offering a different scope of accessibility within packages and inheritance hierarchies.

Why Are Access Modifiers Important?

Access modifiers are essential for writing secure and maintainable Java applications.

Without access control, any part of a program could modify important variables or methods, complicating debugging and increasing security risks.

Here are some key benefits of access modifiers:

  1. Improve encapsulation
  2. Protect sensitive data
  3. Reduce unnecessary dependencies
  4. Enhance code maintainability
  5. Enable controlled inheritance
  6. Prevent accidental changes to program logic

They are widely used in enterprise Java applications, Android development, backend systems, APIs, and frameworks.

Understanding access modifiers also helps developers implement Java design patterns more effectively in scalable applications.

Types of Access Modifiers in Java

Java has four main access modifiers.

Public

The public access modifier allows a class member to be accessed from anywhere in the program.

Private

The private access modifier limits access to the same class only.

Protected

The protected access modifier lets access occur within the same package and through inheritance.

Default

If no access modifier is specified, Java applies default access. Members are only accessible within the same package.

Public Access Modifier

The public access modifier offers the highest level of accessibility.

A public class, method, or variable can be accessed from any other class in the project.

Example of Public Access Modifier

public class Student {
    public String name = “Harini”;

    public void displayName() {
        System.out.println(name);
    }
}

In this case, both the variable and method can be accessed from any class.

When to Use Public Access Modifier

Use public when:

  1. A method should be available globally
  2. APIs need external access
  3. Utility methods are shared across applications
  4. Framework components require public visibility

Private Access Modifier

The private access modifier restricts access to the same class only.

Private members cannot be accessed directly outside the class.

This is commonly used to protect sensitive data.

MDN

Example of Private Access Modifier

In this example, the balance variable cannot be modified directly outside the class.

public class BankAccount {
    private double balance = 5000;

    public double getBalance() {
        return balance;
    }
}

The public getter method provides controlled access.

Why Private Access Matters

Private access improves:

  1. Data hiding
  2. Encapsulation
  3. Application security
  4. Controlled updates to variables

This is one of the most commonly used access modifiers in enterprise applications.

Protected Access Modifier

The protected access modifier allows access:

  1. Within the same package
  2. Inside subclasses through inheritance

This modifier is primarily used with inheritance.

Example of Protected Access Modifier

class Animal {
    protected void sound() {
        System.out.println(“Animal makes sound”);
    }
}

class Dog extends Animal {
    void bark() {
        sound();
    }
}

The Dog class can access the protected method inherited from Animal.

When to Use the Protected Access Modifier

Protected access is useful when:

  1. Building reusable parent classes
  2. Supporting inheritance
  3. Creating framework base classes
  4. Allowing subclass customization

Default Access Modifier

If no access modifier is specified, Java automatically applies default access.

Default members are only accessible within the same package.

Example of Default Access Modifier

class Employee {
    String department = “Engineering”;
}

Here, the department variable can only be accessed inside the same package.

You can strengthen your Java fundamentals further by exploring HCL GUVI’s Java Programs Handbook, which includes commonly asked Java coding programs to improve logic building and programming skills. 

When Default Access Is Useful

Default access is commonly used for:

  1. Package-level utilities
  2. Internal helper classes
  3. Shared package functionality
  4. Restricting external package access

Access Modifier Comparison Table

Access ModifierSame ClassSame PackageSubclassEverywhere
PublicYesYesYesYes
PrivateYesNoNoNo
ProtectedYesYesYesNo
DefaultYesYesNoNo
💡 Did You Know?

Java access modifiers play a crucial role in large-scale software frameworks such as Spring Boot, Hibernate, and the Android SDK, where controlling the visibility of classes, methods, and fields helps maintain clean, secure, and modular architectures. A common enterprise design pattern is to declare fields as private and expose them through public getter and setter methods. This approach supports encapsulation, one of the core principles of object-oriented programming, by preventing direct modification of internal state while allowing developers to validate, monitor, or restrict access to critical business data.

Real World Example of Access Modifiers

Imagine an online banking application.

  1. Account balance should be private to prevent direct modification.
  2. Transaction methods may be public for access across the application.
  3. Internal helper methods may utilize default access.
  4. Protected methods can support specialized account types through inheritance.

This layered access control improves both security and maintainability.

Best Practices for Using Access Modifiers

Choosing the right access modifier is crucial for writing professional Java applications.

Prefer Private for Variables

Most class variables should stay private.

This prevents direct access and protects application data.

Use Public Only When Necessary

Avoid making everything public.

Excessive public access increases coupling and reduces maintainability.

Use Protected Carefully

Protected access should mainly support inheritance.

Avoid unnecessary exposure to subclasses.

Keep Package Utilities Default

Internal helper classes can use default access to minimize external exposure.

Follow Encapsulation Principles

Use private fields with public getter and setter methods whenever necessary.

Using proper access modifiers is an important part of following Java clean coding practices and building maintainable applications. 

Common Mistakes Beginners Make

Many beginners misuse access modifiers while learning Java.

Making All Variables Public

This undermines encapsulation and increases security risks.

Confusing Protected and Default Access

Protected allows subclass access, while default does not.

Overusing Public Methods

Too many public methods make APIs challenging to maintain.

Ignoring Encapsulation

Directly exposing variables can cause unpredictable application behavior.

If you want to improve your Java skills, HCL GUVI’s Java courses can help you learn object-oriented programming, collections, multithreading, Spring Boot, and backend development with hands-on projects.

Conclusion

Access modifiers in Java are vital for controlling visibility, enhancing security, and implementing encapsulation.

By understanding public, private, protected, and default access modifiers, developers can create cleaner and more maintainable applications.

Using the correct access modifier helps prevent accidental misuse of data and supports scalable software design.

Whether you are building beginner Java projects or enterprise applications, mastering access modifiers is essential for becoming a strong Java developer.

FAQs

1. What are access modifiers in Java?

Access modifiers in Java are keywords that control the visibility and accessibility of classes, methods, variables, and constructors.

2. How many access modifiers are available in Java?

Public
Private
Protected
Default

3. What is the difference between private and protected in Java?

Private members are accessible only within the same class, while protected members are accessible within the same package and through inheritance.

4. Which access modifier is the most secure?

Private is considered the most secure because access is limited to the same class only.

MDN

5. Why are access modifiers important in Java?

Access modifiers enhance encapsulation, application security, maintainability, and controlled access to program components.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. TL;DR
  2. Why Are Access Modifiers Important?
  3. Types of Access Modifiers in Java
    • Public
    • Private
    • Protected
    • Default
  4. Public Access Modifier
    • Example of Public Access Modifier
    • When to Use Public Access Modifier
  5. Private Access Modifier
    • Example of Private Access Modifier
    • Why Private Access Matters
  6. Protected Access Modifier
    • Example of Protected Access Modifier
    • When to Use the Protected Access Modifier
  7. Default Access Modifier
    • Example of Default Access Modifier
    • When Default Access Is Useful
  8. Access Modifier Comparison Table
  9. Real World Example of Access Modifiers
  10. Best Practices for Using Access Modifiers
    • Prefer Private for Variables
    • Use Public Only When Necessary
    • Use Protected Carefully
    • Keep Package Utilities Default
    • Follow Encapsulation Principles
  11. Common Mistakes Beginners Make
    • Making All Variables Public
    • Confusing Protected and Default Access
    • Overusing Public Methods
    • Ignoring Encapsulation
  12. Conclusion
  13. FAQs
    • What are access modifiers in Java?
    • How many access modifiers are available in Java?
    • What is the difference between private and protected in Java?
    • Which access modifier is the most secure?
    • Why are access modifiers important in Java?