# Access Modifier

> A word like public or private that says which code may use a part.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** Access Modifiers, Public and Private, Visibility Modifier

## Think of it like

It is like a shop with a front room and a staff-only room. Shoppers can only go in the front.

## Definition

An access modifier is a word that says which code may use a part of a class. public means any code can use it. private means only the class itself can. Java and C# also have protected, for the class and its child classes. Python has no enforced private, so a leading underscore, like _age, asks others to keep out.

## Example

A BankAccount class keeps balance private. Other code must call deposit(), which checks the amount first.

## Example code

```java
public class BankAccount {
    private int balance;
    public void deposit(int amount) {
        if (amount > 0) balance += amount;
    }
}
```

The balance is private. The only way to change it is the public deposit method.

## Why it matters

It lets a class guard its own data. Other code cannot reach in and break the rules.

## Common mistake

Making everything public to save time. Later, any code can change data the class relied on.

## Three principles

- **public.** Any code may use it.
- **private.** Only the class itself may use it.
- **Guards the rules.** Data changes only through methods.

## Related terms

- [Encapsulation](https://learn.benibix.com/term/development/encapsulation)
- [Class](https://learn.benibix.com/term/development/class)
- [Property (Programming)](https://learn.benibix.com/term/development/property)
- [Method (Programming)](https://learn.benibix.com/term/development/method)
- [Inheritance](https://learn.benibix.com/term/development/inheritance)

![A card for a class named Dog. Its top part lists two properties, a public name and a private age. Its lower part lists a method, bark, that uses self.name, and a static method, count, marked as belonging to the class. Two dog objects, Rex and Bella, sit beside it, each with its own name. An arrow from self points at Rex.](https://learn.benibix.com/diagrams/dev7-class-parts.svg)

---

Source: https://learn.benibix.com/term/development/access-modifier
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
