j.l.s 4.12.4 final Variables

hyungjunn·2024년 3월 18일
post-thumbnail

j.l.s 4.12.4 참고

A variable can be declared [[final]]. A final variable may only be assigned to once. It is a compile-time error if a final variable is assigned to unless it is definitely unassigned immediately prior to the assignment (§16 (Definite Assignment)).

변수는 final로 선언될 수 있다. final 변수는 한번만 할당될 수 있다. 할당 직전에 확실히 할당되지 않은 경우 final 변수가 할당되면 컴파일 타임 에러다.

Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.

final 변수가 할당되었을 때, 항상 똑같은 값을 갖는다. 만약 final 변수가 객체에 대한 참조를 가지고 있으면, 객체의 상태는 객체에 대한 연산에 의해 바뀔 수 있으나, 변수는 항상 동일한 객체를 참조할 것이다. 배열은 객체이기 때문에 이 연산은 배열에도 적용된다; 만약 final 변수가 배열에 대한 참조를 가지고 있으면, 배열의 컴포넌트들은 배열에 대한 연산에 의해 바뀔 수 있으나 변수는 항상 같은 배열을 참조한다.

blank final is a final variable whose declaration lacks an initializer.

공백 final은 선언에 초기화가 없는 최종 변수이다.

constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1§13.4.9), and definite assignment (§16 (Definite Assignment)).

상수는 상수 표현식으로 초기화된 primitive type 또는 String type의 final 변수이다. 변수가 상수 변수인지 아닌지는 클래스 초기화, 이진 호환성 및 명확한 할당과 관련하여 영향을 미칠 수 있습니다.

Three kinds of variable are implicitly declared final: a field of an interface (§9.3), a local variable which is a resource of a try-with-resources statement (§14.20.3), and an exception parameter of a multi-catch clause (§14.20). An exception parameter of a uni-catch clause is never implicitly declared final, but may be effectively final.

인터페이스의 필드, [[try-with-resource]]문의 자원인 로컬 변수, multi-catch문의 예외 파라미터는 final로 선언된 세가지 유형의 변수이다. uni-catch의 예외 파라미터는 final로 선언될 수 없으나, effectively final일 수는 있다.

Example 4.12.4-1. Final Variables

Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors. In this program:

class Point {

    int x, y;
    int useCount;
    
    Point(int x, int y) { 
        this.x = x;
        this.y = y;
    }
    
    static final Point origin = new Point(0, 0);
}

the class Point declares a final class variable origin. The origin variable holds a reference to an object that is an instance of class Point whose coordinates are (0, 0). The value of the variable Point.origin can never change, so it always refers to the same Point object, the one created by its initializer. However, an operation on this Point object might change its state - for example, modifying its useCount or even, misleadingly, its x or y coordinate.

Point 클래스는 final 클래스 변수 origin을 선언한다. 좌표 (0, 0)을 가지는 Point 클래스의 인스턴스에 대한 참조를 origin 변수가 가지고 있다. 그러나, Point 객체에 대한 작업은 그것의 상태를 바꿀 수 있다. - 예를 들어, useCount를 수정하거나 또는 심지어 오해의 소지가 있는 x 또는 y 좌표.

Certain variables that are not declared final are instead considered effectively final:

final로 선언되지 않은 특정 변수들은 effectively final을 고려할 수 있다:

  • A local variable declared by a statement and whose declarator has an initializer (§14.4), or a local variable declared by a pattern (§14.30.1), is effectively final if all of the following are true:
  • 문에 의해 선언되고 선언자가 초기화를 가지고 있는 지역변수 또는 패턴에 의해 선언된 지역변수는 만약 아래의 모든 것을 충족시키면 [[effectively final]] 이다.
    • It is not declared final.
    • final로 선언되지 않는다.
    • It never occurs as the left hand side in an assignment expression (§15.26). (Note that the local variable declarator containing the initializer is not an assignment expression.)
    • 할당 표현에서 왼쪽으로 절대 발생하지 않는다. (초기화를 포함하는 지역 변수 선언자는 할당 표현이 아니다.)
    • It never occurs as the operand of a prefix or postfix increment or decrement operator (§15.14§15.15).
    • 접두사 또는 최종 접미사 증가 또는 감소 연산자의 피연산자로 발생하지 않는다.
  • A local variable declared by a statement and whose declarator lacks an initializer is effectively final if all of the following are true:
  • 초기화가 없는 선언자그리고 문에 의해 선언된 지역변수는 아래 사항을 충족시키면 effectively final이다.
    • It is not declared final.
    • final로 선언되지 않는다.
    • Whenever it occurs as the left hand side in an assignment expression, it is definitely unassigned and not definitely assigned before the assignment; that is, it is definitely unassigned and not definitely assigned after the right hand side of the assignment expression (§16 (Definite Assignment)).
    • 할당 표현식에서 왼쪽으로 발생할 때마다 확실히 할당되지 않고 할당 전에 확실히 할당되지 않는다. 즉, 그것은 확실히 할당되지 않으며 할당 표현식의 오른쪽 뒤에 확실히 할당되지 않는다.
    • It never occurs as the operand of a prefix or postfix increment or decrement operator.
    • 접두사 또는 최종 접미사의 증가 또는 감소 연산자의 피연산자로 발생하지 않는다.
  • A method, constructor, lambda, or exception parameter (§8.4.1§8.8.1§9.4§15.27.1§14.20) is treated, for the purpose of determining whether it is effectively final, as a local variable whose declarator has an initializer.
  • 메서드, 생성자, 람다, 또는 예외 파라미터는 선언자가 초기화를 가지고 있는 지역변수로서 effectively final인지 여부를 결정할 목적으로 처리한다.

If a variable is effectively final, adding the final modifier to its declaration will not introduce any compile-time errors. Conversely, a local variable or parameter that is declared final in a valid program becomes effectively final if the final modifier is removed.

변수가 effectively final이라면, 선언에 final 수정자를 추가하면 컴파일 타임 에러가 발생하지 않는다. 반대로, 유효한 프로그램에서 effectively final로 선언된 지역 변수 또는 파라미터는 final 수정자가 제거되면 effectively final이 된다.

0개의 댓글