GoBasics

Java Access Modifiers Explained: With Syntax, Examples & Outputs

In Java, access modifiers decide who can use or see certain parts of your code. They’re like visibility controls: do you want this method public for all to use, or private and hidden inside the class? This guide will help you understand each modifier clearly.

Java Access Modifiers Explained Simply

When you're writing a class or method in Java, sometimes you want to control who can use it.
Should everyone be allowed to use it? Only the same class? Or just within the same package?

This is where access modifiers come in.
They help you decide what is visible and what should be hidden.

Types of Access Modifiers

Java provides four main access levels:

1. The public modifier

The public keyword tells Java, “This part of the code should be visible to everyone.”
That means any class, from anywhere in the project, can access it.

Let’s say we want everyone to access a method from anywhere—even from another package.

public class Greeting {
    public void sayHello() {
        System.out.println("Hello, everyone!");
    }
    }
Greeting g = new Greeting();
    g.sayHello(); //  Works fine

Here, the method is public, so it’s available everywhere.

2. The private modifier

The private modifier tells Java, “Keep this just for this class.”
This is the most restrictive access.

It means that no one else—not even other classes in the same package—can touch it directly.

public class Person {
    private String name = "John";
      public String getName() {
          return name;
      }
    }
Person p = new Person();
    // System.out.println(p.name); Error - name is private
    System.out.println(p.getName()); // Correct way

We used a getter method to access the private variable. This is common in real-world Java code.
Only the same class can use or update this variable.

3. The protected modifier

The protected modifier sits in between private and public.
It tells Java, “Allow access within the same package, and also from subclasses.”

Let’s see an example using inheritance.

public class Animal {
    protected void eat() {
          System.out.println("Animal is eating");
      }
    }

    public class Dog extends Animal {
      public void makeDogEat() {
          eat(); // Allowed because it's a subclass
      }
    }

Even though eat() is not public, the subclass can access it because it's protected.
If we tried to use eat() from an unrelated class outside the package, it wouldn’t work.

4. Default access (No modifier)

Sometimes, you won’t write anything before your method or variable—not even public or private.
In that case, Java gives it what’s called default access (also known as package-private).

That simply means: “This can be accessed only within the same package.”

class Bike {
      void start() {
          System.out.println("Bike is starting");
      }
    }
Bike b = new Bike();
b.start(); //  Works

But if you move this usage to a different package, it won’t compile.
That’s because default access only works within the same package.

Quick Comparison Table

Modifier Same Class Same Package Subclass Outside Package
public Yes Yes Yes Yes
private Yes No No No
protected Yes Yes Yes No
default Yes Yes No No

Official Java Documentation: Java Access Control

Related Posts

Java Methods Explained

Understand how to create reusable blocks of code with Java methods. Learn syntax, parameters, return types, and method overloading.

Read More

Java Control Statements

Explore Java’s control structures like if-else, switch, and loops to manage program flow.

Read More

Java Access Modifiers Explained

Learn how Java's access modifiers (public, private, protected, default) control the visibility and accessibility of classes, methods, and fields.

Read More