Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Understanding Java Access Modifiers: Types, Rules, and Practical Examples

Java Access Modifiers

This tutorial explores Java access modifiers, their four types, and demonstrates their usage through clear examples.

What are Access Modifiers?

In Java, access modifiers define the visibility of classes, interfaces, variables, methods, constructors, and data members. For example,

class Animal {
    public void method1() {...}
    private void method2() {...}
}

In the example above, method1 is public – accessible from any class. method2 is private – restricted to the enclosing class.

These keywords – public and private – are the core visibility modifiers in Java.

Note: Getter methods themselves cannot be declared with an access modifier.


Types of Access Modifiers

Before diving into the modifiers, ensure you understand Java packages.

Java offers four primary visibility modifiers:

ModifierDescription
Default (package‑private)Visible only within the same package.
PrivateVisible solely within the declaring class.
ProtectedVisible within the package and to subclasses in other packages.
PublicVisible everywhere.

Default Access Modifier

When no modifier is specified, Java applies the default (package‑private) visibility. For instance:

package defaultPackage;
class Logger {
    void message() {
        System.out.println("This is a message");
    }
}

The Logger class is accessible only to classes within defaultPackage. Using it from another package results in a compilation error.


Private Access Modifier

Declaring a member as private confines its access to the containing class:

class Data {
    private String name;
}
public class Main {
    public static void main(String[] args) {
        Data d = new Data();
        d.name = "Programiz"; // ❌ compile‑time error
    }
}

The error occurs because name is private to Data. To expose it safely, provide accessor methods:

class Data {
    private String name;
    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
}
public class Main {
    public static void main(String[] args) {
        Data d = new Data();
        d.setName("Programiz");
        System.out.println(d.getName());
    }
}

Output:

The name is Programiz

Note that nested classes can be declared private, but top‑level classes and interfaces cannot.

Note: Java does not allow top‑level classes or interfaces to be private.


Protected Access Modifier

Members marked protected are visible within the same package and to subclasses in other packages. Example:

class Animal {
    protected void display() {
        System.out.println("I am an animal");
    }
}
class Dog extends Animal {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.display(); // ✅ allowed
    }
}

Output:

I am an animal

Protected members cannot be declared on top‑level classes or interfaces.

Note: Top‑level classes and interfaces cannot be protected.


Public Access Modifier

Members declared public are universally accessible:

// Animal.java
public class Animal {
    public int legCount;
    public void display() {
        System.out.println("I am an animal.");
        System.out.println("I have " + legCount + " legs.");
    }
}
// Main.java
public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.legCount = 4;
        animal.display();
    }
}

Output:

I am an animal.
I have 4 legs.

Summary: Animal (public class), legCount (public field), and display() (public method) are all accessible from Main.


Access Modifiers Summarized in One Figure

Understanding Java Access Modifiers: Types, Rules, and Practical Examples

Access modifiers underpin encapsulation, enabling developers to control visibility and protect data integrity. For more on encapsulation, see Java Encapsulation.

Java

  1. Understanding C# Access Modifiers: Public, Private, Protected, Internal, and More
  2. Master Java Operators: Types, Syntax, & Practical Examples
  3. Understanding Java Access Modifiers: Types, Rules, and Practical Examples
  4. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  5. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  6. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
  7. Java Annotations Explained: Types, Placement, and Practical Examples
  8. Understanding C# Access Modifiers (Specifiers) with Practical Examples
  9. Java Modifiers Explained: Types, Usage, and Best Practices
  10. Java 9 Private Interface Methods: Boosting Encapsulation & Code Reuse