---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 13. Binary Compatibility"
section: "13.4.9"
heading: "13.4.9. final Fields and static Constant Variables"
parent: "13.4. Evolution of Classes"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-13.html#jls-13.4.9
id: "13.4.9"
url: https://jvm.obfuscated.software/jls/se8/13.4.9.html
raw: https://jvm.obfuscated.software/jls/se8/13.4.9.md
---

# 13.4.9. `final` Fields and `static` Constant Variables

If a field that was not declared `final` is changed to be declared `final`, then it can break compatibility with pre-existing binaries that attempt to assign new values to the field.

**Example 13.4.9-1. Changing A Variable To Be `final`**

```
class Super { char s; }
class Test extends Super {
    public static void main(String[] args) {
        Super x = new Super();
        x.s = 'a';
        System.out.println(x.s);
    }
}
```

This program produces the output:

```
a
```

Suppose that a new version of class `Super` is produced:

```
class Super { final char s = 'b'; }
```

If `Super` is recompiled but not `Test`, then running the new binary with the existing binary of `Test` results in a `IllegalAccessError`.

Deleting the keyword `final` or changing the value to which a field is initialized does not break compatibility with existing binaries.

If a field is a constant variable ([§4.12.4](4.12.4.md)), and moreover is `static`, then deleting the keyword `final` or changing its value will not break compatibility with pre-existing binaries by causing them not to run, but they will not see any new value for a usage of the field unless they are recompiled. This result is a side-effect of the decision to support conditional compilation ([§14.21](14.21.md)). (One might suppose that the new value is not seen if the usage occurs in a constant expression ([§15.28](15.28.md)) but is seen otherwise. This is not so; pre-existing binaries do not see the new value at all.)

Another reason for requiring inlining of values of `static` constant variables is because of `switch` statements. They are the only kind of statement that relies on constant expressions, namely that each `case` label of a `switch` statement must be a constant expression whose value is different than every other `case` label. `case` labels are often references to `static` constant variables so it may not be immediately obvious that all the labels have different values. If it is proven that there are no duplicate labels at compile time, then inlining the values into the `class` file ensures there are no duplicate labels at run time either - a very desirable property.

**Example 13.4.9-2. Conditional Compilation**

If the example:

```
class Flags { static final boolean debug = true; }
class Test {
    public static void main(String[] args) {
        if (Flags.debug)
            System.out.println("debug is true");
    }
}
```

is compiled and executed, it produces the output:

```
debug is true
```

Suppose that a new version of class `Flags` is produced:

```
class Flags { static final boolean debug = false; }
```

If `Flags` is recompiled but not `Test`, then running the new binary with the existing binary of `Test` produces the output:

```
debug is true
```

because the value of `debug` was a constant expression, and could have been used in compiling `Test` without making a reference to the class `Flags`.

This behavior would not change if `Flags` were changed to be an interface, as in the modified example:

```
interface Flags { boolean debug = true; }
class Test {
    public static void main(String[] args) {
        if (Flags.debug)
            System.out.println("debug is true");
    }
}
```

Conditional compilation is discussed further at the end of [§14.21](14.21.md).

The best way to avoid problems with "inconstant constants" in widely-distributed code is to use `static` constant variables only for values which truly are unlikely ever to change. Other than for true mathematical constants, we recommend that source code make very sparing use of `static` constant variables.

If the read-only nature of `final` is required, a better choice is to declare a `private` `static` variable and a suitable accessor method to get its value. Thus we recommend:

```
private static int N;
public static int getN() { return N; }
```

rather than:

```
public static final int N = ...;
```

There is no problem with:

```
public static int N = ...;
```

if `N` need not be read-only.

We recommend, as a general rule, that only constant expressions be assigned to fields of interfaces.

We note, but do not recommend, that if a field of primitive type of an interface may change, its value may be expressed idiomatically as in:

```
interface Flags {
    boolean debug = new Boolean(true).booleanValue();
}
```

ensuring that this value is not a constant. Similar idioms exist for the other primitive types.

One other thing to note is that `static` constant variables must never appear to have the default initial value for their type ([§4.12.5](4.12.5.md)). This means that all such fields appear to be initialized first during class initialization ([§8.3.2](8.3.2.md), [§9.3.1](9.3.1.md), [§12.4.2](12.4.2.md)).
