Access Modifiers in Java
Published: (February 17, 2026 at 05:18 AM EST)
3 min read
Source: Dev.to
Source: Dev.to
[](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 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
privateorprotectedbecause those modifiers refer to visibility relative to an enclosing class or inheritance hierarchy. - Inner (nested) classes can be declared
privateorprotected.
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");
}
}