access java modifiers

What are Access Modifiers in Java?

April 3rd, 2026
4421
5:00 Minutes

Many Java developers often struggle to understand why some parts of their code can be accessed from anywhere, while others throw errors when you try to use them from a different class. You might also have faced confusion about visibility in your programs. That’s exactly where Access Modifiers in Java come into play.

Access modifiers are special keywords that control who can see and use your classes, methods, variables, and constructors. They help you build secure, organized, and professional code by deciding the level of access, from completely hidden to openly available. It also has an important role in object-oriented programming (OOP).

In this guide, I’ll walk you through everything you need to know about access modifiers in Java, including the four types (public, private, protected, and default), how they actually work with clear examples, key differences, best practices, and common mistakes that trip up both beginners and experienced developers.

Read Also- Java Tutorial

What are Access Modifiers in Java?

Access modifiers are keywords in Java that set the accessibility (visibility) of classes, interfaces, variables, methods, and constructors. It specifies which parts of the program can interact with or access them. It also defines the scope and behaviour of these components, along with accessibility.

Modifiers in Java form the groundwork for building secure code while building large applications.

Example of Access Modifier

Let's take a look at the following example of access modifiers to understand it better.

// In the same package
class Demo {
public int publicVar = 1;
private int privateVar = 2;
protected int protectedVar = 3;
int defaultVar = 4; // package-private (default)
}
// Accessing within the same class
public class Test {
public static void main(String[] args) {
Demo demo = new Demo();
System.out.println(demo.publicVar); // OK
System.out.println(demo.privateVar); // OK (same class)
System.out.println(demo.protectedVar); // OK
System.out.println(demo.defaultVar); // OK
}
}

Explanation

  • publicVar- accessible everywhere
  • privateVar- accessible only within its own class
  • protectedVar- accessible within its package or by subclasses
  • defaultVar- accessible only within its package

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Types of Access Modifiers in Java

Types of Access Modifiers

As discussed above, Java Modifiers are in charge of determining the visibility of classes, methods and variables. It is also important to note the other four types of access modifiers-

1. Default Access Modifier

When you do not directly specify a class, method and field in Java, it naturally gets the default access called package-private. It means the element is accessible only by other classes within the same package. Other packages cannot access it.

Example

We are going to make two packages in this example. Both packages have classes with default access and we try to access a class from one package within a class in the other package.

igm.java

// default access modifier

package p1;

// Class igm is having

// Default access modifier

class igm

{

void display()

{

System.out.println("Hello World!");

}

}

igmNew.Java

// error while using class from different

// package with default modifier

package p2;

import p1.*; // importing package p1

// This class is having

// default access modifier

class igmNew {

public static void main(String args[]) {

// Accessing class igm from package p1

igm o = new igm();

o.display();

}

}

Explanation

Attempting to access a class with default access from a different package results in a compile-time error in the above program.

Also Explore: Java Cheat Sheet

2. Private Access Modifier

The private access modifier in Java is specified with the private keyword. It restricts access to methods and data members so that they are only accessible within their own class.

In Java, top-level classes and interfaces cannot be declared as private or protected. The private modifier means visibility is limited to the enclosing class. The protected modifier means that it allows access within the same package.

Example

In this example, the private access modifier ensures that the field and method in the igmGuru class are only accessible from within that class.

class igmGuru {

private String secretTip = "Use private wisely!";

private void whisperTip() {

System.out.println(secretTip);

}

public void shareTip() {

whisperTip(); // OK: private method accessed from within the class

}

}

public class Main {

public static void main(String[] args) {

igmGuru guru = new igmGuru();

guru.shareTip(); // Works: uses public method that internally accesses private members

// guru.secretTip; // Compile-time error: secretTip has private access in igmGuru

// guru.whisperTip(); // Compile-time error: whisperTip() has private access in igmGuru

}

}

Explanation

Only the shareTip() public method can internally invoke the private whisperTip() and access the secretTip.

3. Public Access Modifier

The public access modifier is declared using the public keyword. It has the broadest visibility in Java among all other modifiers. Classes, fields and methods marked as public can be accessed from anywhere in the program without any blockages.

Example

Here is an example of the code showing that a public method is accessible within the same package.

// public modifier

package p1;

public class A {

public void display() {

System.out.println("igmGuru");

}

}

4. Protected Access Modifier

You specify the protected access modifier using the protected keyword in Java. Members declared protected are accessible within the same package and also by subclasses, even if they reside in different packages.

Example

In this example, a subclass in a different package is allowed to use the parent class's protected method.

// In package animals

package animals;

public class Animal {

protected void makeSound() {

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

}

}

// In package zoo

package zoo;

import animals.Animal;

public class ZooKeeper extends Animal {

public void demonstrate() {

makeSound(); // Allowed: accessing protected method via inheritance

}

public static void main(String[] args) {

new ZooKeeper().demonstrate();

}

}

Explanation

The protected method makesound() in Animal is accessible to its subclass zooKeeper even though it resides in a different package. It showcases permission granted through inheritance.

Read Also- Java Interview Questions and Answers

What are Non-Access Modifiers in Java?

Non-access modifiers in Java bring additional capabilities. This includes managing inheritance, allowing immutability and specifying special behaviour for components. Here are some common non-access modifiers -

  • Abstract- Marks a class or method as incomplete. An abstract method has no body and must be implemented by a subclass. An abstract class cannot be instantiated directly.
  • Volatile- It makes sure that a variable's value is always read directly from main memory and gives updated data to all threads.
  • Final- It used to make something unchangeable. For example, a class that cannot be extended or a method that cannot be overridden.
  • Transient- Prevents a field from being serialized when an object is converted to a byte stream.
  • Synchronized- It makes sure only one thread can execute a method or block at a time.
  • Static- It indicates a member belongs to the class itself and not to any specific object.

Systematic Use of Modifiers in Java

Without knowing how to use Modifiers in Java, there will be no use of this knowledge. You will need to follow a systematic approach to do it. It requires you to follow the given strategy:

1. Begin with your Requirements

Understand what parts of your system should be publicly accessible versus internal. This assessment will guide your access level choices for each class, method and field.

2. Default to the most restrictive modifier

Start with private wherever possible and only widen the scope if necessary. This helps reduce unintended interactions and maintain encapsulation.

  • Methods and Fields- Keep them private unless they need access from subclasses or other classes.
  • Classes- Use package private unless external use is required.

3. Use protected for inheritance

Apply protection to members that should be accessible within subclasses. This also includes those in other packages but remain hidden from unrelated classes.

4. Apply public selectively

Reserve public access for methods and fields that form your external API. It is intended for use across different packages or systems. You must avoid making everything public.

5. Document your choices

Annotate each choice with a brief rationale. Future developers can then understand why a member is private versus public, which supports architectural consistency.

Want to Learn Everything About Java Programming?

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Wrapping Up: Modifiers in Java

It is safe to conclude that Java Modifiers are more than just normal keywords. They lay the groundwork for building clean, secure and maintainable code. I hope this guide was helpful enough to understand what modifiers in java matters and how mastering these would assist you in writing effective code.

FAQs: Modifiers in Java

Q1. Can a top-level class have a private or protected modifier?

A top-level class can only be public or default (package private). Inner (nested) classes can have any access modifier.

Q2. What is the default access level when no modifier is specified?

The default access level is package private when no access modifier is specified. This means the class or method is only accessible within classes in the same package.

Q3. Which modifier is the most secure in Java?

The private modifier provides the highest level of restriction because it limits access to within the same class only.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled Data Analyst with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.