final

seheeee_97·2023년 11월 21일
0

개인 공부

목록 보기
9/44

final = 변수를 수정할 수 없도록 선언

public class Example {
    private final int readOnlyVariable;

    public Example(int value) {
        this.readOnlyVariable = value;
    }

    public int getReadOnlyVariable() {
        return readOnlyVariable;
    }
}

사용하지 않는 경우

public class Example {
    private int variable;

    public Example(int value) {
        this.variable = value;
    }

    public int getVariable() {
        return variable;
    }

    public void setVariable(int newValue) {
        this.variable = newValue;
    }
}

여기서 variable은 final로 선언되지 않아서 setVariable 메서드를 통해 언제든지 변경될 수 있음


메서드나 클래스에서 final을 사용하면 오버라이드나 상속할 수 없게 함.

0개의 댓글