- 最終更新
- 2026/08/17
3秒でわかる
クラスの中身をどこから触れるかを決めるキーワード。public と private を使い分けて、壊されたくない部分を外から隠すために使います。
30秒図解
もう少し詳しく
どういうものか
アクセス修飾子は、クラスやフィールド、メソッドに付けて、
範囲は
public どこからでも
protected 同じパッケージ + サブクラス
(無指定) 同じパッケージのみ
private 同じクラス内のみTypeScript にも#field という
なぜ必要か
フィールドをbalance が
もう 1 つは変更の
具体例
public class BankAccount {
private int balance; // 外から直接触れない
public BankAccount(int initial) {
if (initial < 0) throw new IllegalArgumentException("初期残高が負です");
this.balance = initial;
}
public void withdraw(int amount) {
if (amount <= 0) throw new IllegalArgumentException("金額が不正です");
if (amount > balance) throw new IllegalStateException("残高不足です");
balance -= amount;
}
public int getBalance() {
return balance;
}
}account.balance = -500; はwithdraw だけなので、
つまずきやすいところ
Java で
もう 1 つは、
似た用語との違い
| 用語 | 内容 |
|---|---|
| アクセス修飾子 | 可視範囲を |
| カプセル化 | 内部を |
| final | 再代入や |
覚え方
既定は


