---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 20 Edition"
java_version: 20
chapter: "Chapter 13. Binary Compatibility"
section: "13.4.8"
heading: "13.4.8. Field Declarations"
parent: "13.4. Evolution of Classes"
source: https://docs.oracle.com/javase/specs/jls/se20/html/jls-13.html#jls-13.4.8
id: "13.4.8"
url: https://jvm.obfuscated.software/jls/se20/13.4.8.html
raw: https://jvm.obfuscated.software/jls/se20/13.4.8.md
---

# 13.4.8. Field Declarations

Widely distributed programs should not expose any fields to their clients. Apart from the binary compatibility issues discussed below, this is generally good software engineering practice. Adding a field to a class may break compatibility with pre-existing binaries that are not recompiled.

Assume a reference to a field `f` with qualifying class C. Assume further that `f` is in fact an instance (respectively `static`) field declared in a superclass of C, S, and that the type of `f` is X.

If a new field of type X with the same name as `f` is added to a subclass of S that is a superclass of C or C itself, then a linkage error may occur. Such a linkage error will occur only if, in addition to the above, either one of the following is true:

- The new field is less accessible than the old one.
- The new field is a `static` (respectively instance) field.

In particular, no linkage error will occur in the case where a class could no longer be recompiled because a field access previously referenced a field of a superclass with an incompatible type. The previously compiled class with such a reference will continue to reference the field declared in a superclass.

**Example 13.4.8-1. Adding A Field Declaration**

```
class Hyper { String h = "hyper"; }
class Super extends Hyper { String s = "super"; }
class Test {
    public static void main(String[] args) {
        System.out.println(new Super().h);
    }
}
```

This program produces the output:

```
hyper
```

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

```
class Super extends Hyper {
    String s = "super";
    int h = 0;
}
```

Then, recompiling `Hyper` and `Super`, and executing the resulting new binaries with the old binary of `Test` produces the output:

```
hyper
```

The field `h` of `Hyper` is output by the original binary of `Test`. While this may seem surprising at first, it serves to reduce the number of incompatibilities that occur at run time. (In an ideal world, all source files that needed recompilation would be recompiled whenever any one of them changed, eliminating such surprises. But such a mass recompilation is often impractical or impossible, especially in the Internet. And, as was previously noted, such recompilation would sometimes require further changes to the source code.)

As another example, if the program:

```
class Hyper { String h = "Hyper"; }
class Super extends Hyper { }
class Test extends Super {
    public static void main(String[] args) {
        String s = new Test().h;
        System.out.println(s);
    }
}
```

is compiled and executed, it produces the output:

```
Hyper
```

Suppose that a new version of class `Super` is then compiled:

```
class Super extends Hyper { char h = 'h'; }
```

If the resulting binary is used with the existing binaries for `Hyper` and `Test`, then the output is still:

```
Hyper
```

even though compiling the source for these binaries:

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

would result in a compile-time error, because the `h` in the source code for `main` would now be construed as referring to the `char` field declared in `Super`, and a `char` value can't be assigned to a `String`.

Deleting a field from a class will break compatibility with any pre-existing binaries that reference this field, and a `NoSuchFieldError` will be thrown when such a reference from a pre-existing binary is linked. Only `private` fields may be safely deleted from a widely distributed class.

For purposes of binary compatibility, adding or deleting a field `f` whose type involves type variables ([§4.4](4.4.md)) or parameterized types ([§4.5](4.5.md)) is equivalent to the addition (respectively, deletion) of a field of the same name whose type is the erasure ([§4.6](4.6.md)) of the type of `f`.
