
j.l.s 4.12.4 참고
A variable can be declared [[final]]. A
finalvariable may only be assigned to once. It is a compile-time error if afinalvariable is assigned to unless it is definitely unassigned immediately prior to the assignment (§16 (Definite Assignment)).
변수는 final로 선언될 수 있다. final 변수는 한번만 할당될 수 있다. 할당 직전에 확실히 할당되지 않은 경우 final 변수가 할당되면 컴파일 타임 에러다.
Once a
finalvariable has been assigned, it always contains the same value. If afinalvariable 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 afinalvariable 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 변수가 배열에 대한 참조를 가지고 있으면, 배열의 컴포넌트들은 배열에 대한 연산에 의해 바뀔 수 있으나 변수는 항상 같은 배열을 참조한다.
A blank
finalis afinalvariable whose declaration lacks an initializer.
공백 final은 선언에 초기화가 없는 최종 변수이다.
A constant variable is a
finalvariable of primitive type or typeStringthat 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 atry-with-resources statement (§14.20.3), and an exception parameter of a multi-catchclause (§14.20). An exception parameter of a uni-catchclause is never implicitly declaredfinal, but may be effectively final.
인터페이스의 필드, [[try-with-resource]]문의 자원인 로컬 변수, multi-catch문의 예외 파라미터는 final로 선언된 세가지 유형의 변수이다. uni-catch의 예외 파라미터는 final로 선언될 수 없으나, effectively final일 수는 있다.
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
finalare instead considered effectively final:
final로 선언되지 않은 특정 변수들은 effectively final을 고려할 수 있다:
final.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이 된다.