접근 제어자


<aside> 🃏

예시


package access;

public class Speaker {

    int volume;

    Speaker(int volume) {
        this.volume = volume;
    }

    void volumeUp() {
        if(volume >= 100) {
            System.out.println("최대 음량");
        } else {
            volume += 10;
            System.out.println("스피커 음량 증가");
        }
    }

    void volumeDown() {
        volume -= 10;
        System.out.println("스피커 음량 감소");
    }

    void showVolume() {
        System.out.println("현재 스피커 음량 : " + volume);
    }
}
----------------------
package access;

public class SpeakerMain {
    public static void main(String[] args) {
        Speaker speaker = new Speaker(90);
        speaker.volumeUp();
        speaker.showVolume();
    }
}

그런데 여기서 문제점
speaker.volume = 150;
speaker.showVolume();
볼륨 값에 직접 값을 바꾸면 100을 증가가 됌
해당 문제를 막기 위한 것이 접근 제어자이고 다음 페이지에서 알아봄

image.png

</aside>