---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 22 Edition"
java_version: 22
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.12.4"
heading: "4.12.4. final Variables"
parent: "4.12. Variables"
source: https://docs.oracle.com/javase/specs/jls/se22/html/jls-4.html#jls-4.12.4
id: "4.12.4"
url: https://jvm.obfuscated.software/jls/se22/4.12.4.html
raw: https://jvm.obfuscated.software/jls/se22/4.12.4.md
---

# 4.12.4. `final` Variables

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*)](16.md)).

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.

A *blank `final`* is a `final` variable whose declaration lacks an initializer.

A *constant variable* is a `final` variable of primitive type or type `String` that is initialized with a constant expression ([§15.29](15.29.md)). Whether a variable is a constant variable or not may have implications with respect to class initialization ([§12.4.1](12.4.1.md)), binary compatibility ([§13.1](13.1.md)), reachability ([§14.22](14.22.md)), and definite assignment ([§16.1.1](16.1.1.md)).

Three kinds of variable are implicitly declared `final`: a field of an interface ([§9.3](9.3.md)), a local variable declared as a resource of a `try`-with-resources statement ([§14.20.3](14.20.3.md)), and an exception parameter of a multi-`catch` clause ([§14.20](14.20.md)). An exception parameter of a uni-`catch` clause is never implicitly declared `final`, but may be 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.

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

- A local variable declared by a statement and whose declarator has an initializer ([§14.4](14.4.md)), or a local variable declared by a pattern ([§14.30.1](14.30.1.md)), is *effectively final* if all of the following are true:

  - It is not declared `final`.
  - It never occurs as the left hand side in an assignment expression ([§15.26](15.26.md)). (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.14.md), [§15.15](15.15.md)).

- A local variable declared by a statement and whose declarator lacks an initializer is *effectively final* if all of the following are true:

  - It is not declared `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*)](16.md)).
  - 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.4.1.md), [§8.8.1](8.8.1.md), [§9.4](9.4.md), [§15.27.1](15.27.1.md), [§14.20](14.20.md)) is treated, for the purpose of determining whether it is *effectively final*, as a local variable whose declarator has an initializer.

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.
