Access Modifiers in Java

Published: (February 17, 2026 at 05:18 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

[![Nanthini Ammu](https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3734090%2F5e58c4e3-11b0-493a-8b5b-c1bd75497468.png)](https://dev.to/nanthini_ammu_ac02ad32802)

## Access Modifiers

- Control visibility and scope.
- Define who can access classes, methods, and variables.
- Enforce encapsulation and data hiding.
- Four types: **public**, **protected**, **default** (package‑private), and **private**.

### Best Practices

- Use **private** for fields; expose them via getters/setters.
- Use **public** for methods that are meant to be reused.
- Use **protected** carefully in inheritance hierarchies.
- Avoid **default** unless package‑level access is intentional.

![Access Modifiers Diagram](https://media2.dev.to/dynamic/image/width=800,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxb63g17sbl8lidf82pz.png)

## Access Modifiers on Instance/Object Variables & Static Variables

### `private`

- Accessible only inside the same class.
- Access from outside must go through a getter/setter.
- Used to achieve encapsulation.

```java
private int salary;

default (no modifier)

  • Accessible only within the same package.
  • Also called package‑private.
int salary;   // package‑private

protected

  • Accessible within the same package.
  • Also accessible in subclasses (even in different packages).
  • Primarily used for inheritance.
protected int salary;

public

  • Accessible from anywhere.
  • No restriction, but not recommended for sensitive data.
public int salary;

Real‑World Best Practice: Private fields + public getter/setter

public class Employee {

    private int salary;   // hidden variable

    // Getter (read value)
    public int getSalary() {
        return salary;
    }

    // Setter (modify value)
    public void setSalary(int salary) {
        this.salary = salary;
    }
}

Access Modifiers in Classes

public

  • Accessible from any package.
  • The file name must match the public class name.
  • Only one public class is allowed per file.
public class Employee {
    void display() {
        System.out.println("Public Class");
    }
}

default (no modifier)

  • Accessible only within the same package.
  • Not visible outside the package.
class Employee {
    void display() {
        System.out.println("Default Class");
    }
}

private & protected (top‑level classes)

  • A top‑level class cannot be private or protected because those modifiers refer to visibility relative to an enclosing class or inheritance hierarchy.
  • Inner (nested) classes can be declared private or protected.

Access Modifiers on Methods

private

  • Accessible only inside the same class.
private void calculateSalary() {
    System.out.println("Salary Calculated");
}

default (no modifier)

  • Accessible only within the same package.
void display() {
    System.out.println("Default Method");
}

protected

  • Accessible within the same package and in subclasses (even in different packages).
protected void showDetails() {
    System.out.println("Employee Details");
}

public

  • Accessible from anywhere.
public void display() {
    System.out.println("Public Method");
}

Access Modifiers on Constructors

private

  • Objects cannot be created outside the class.
  • Useful for the Singleton pattern and utility classes.
class Company {
    private Company() {
        System.out.println("Private Constructor");
    }
}

default (no modifier)

  • Objects can be created only within the same package.
class Employee {
    Employee() {
        System.out.println("Employee Created");
    }
}

protected

  • Accessible within the same package and in subclasses (even in different packages).
class Employee {
    protected Employee() {
        System.out.println("Protected Constructor");
    }
}

class Manager extends Employee {
    Manager() {
        super();   // calls the protected constructor
    }
}

## Allowed

```text
}
}

public :

  • Object can be created from anywhere.
  • Most commonly used.
  • Accessible from any package.
class Employee {

    public Employee() {
        System.out.println("Employee Created");
    }
}
0 views
Back to Blog

Related posts

Read more »

Scanner Class

Class in Java Definition of the class concept in Java. Why Scanner? The Scanner class simplifies reading input from various sources, such as the keyboard. How...