---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 15. Expressions"
section: "15.11"
heading: "15.11. Field Access Expressions"
parent: "Chapter 15. Expressions"
source: https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.11
id: "15.11"
url: https://jvm.obfuscated.software/jls/se27/15.11.html
raw: https://jvm.obfuscated.software/jls/se27/15.11.md
---

# 15.11. Field Access Expressions

A field access expression may access a field of an object or array, a reference to which is the value of either an expression or the special keyword `super`.

```
FieldAccess:
  Primary . Identifier
  super . Identifier
  TypeName . super . Identifier
```

The meaning of a field access expression is determined using the same rules as for qualified names ([§6.5.6.2](6.5.6.2.md)), but limited by the fact that an expression cannot denote a package, class type, or interface type.

It is also possible to refer to a field of the current instance or current class by using a simple name ([§6.5.6.1](6.5.6.1.md)).

## 15.11.1. Field Access Using a Primary

The type of the *Primary* must be a reference type T, or a compile-time error occurs.

The meaning of the field access expression is determined as follows:

- If the identifier names several accessible ([§6.6](6.6.md)) member fields in type T, then the field access is ambiguous and a compile-time error occurs.
- If the identifier does not name an accessible member field in type T, then the field access is undefined and a compile-time error occurs.
- Otherwise, the identifier names a single accessible member field in type T, and the type of the field access expression is the type of the member field after capture conversion ([§5.1.10](5.1.10.md)).

At run time, the result of the field access expression is computed as follows: (assuming that the program is correct with respect to definite assignment analysis, that is, every blank `final` variable is definitely assigned before access)

- If the field is `static`:

  - The *Primary* expression is evaluated, and the result is discarded. If evaluation of the *Primary* expression completes abruptly, the field access expression completes abruptly for the same reason.
  - If the field is a non-blank `final` field, then the result is the value of the specified class variable in the class or interface that is the type of the *Primary* expression.
  - If the field is not `final`, or is a blank `final` and the field access occurs in a class variable initializer ([§8.3.2](8.3.2.md)) or static initializer ([§8.7](8.7.md)), then the result is a variable, namely, the specified class variable in the class that is the type of the *Primary* expression.

- If the field is not `static`:

  - The *Primary* expression is evaluated. If evaluation of the *Primary* expression completes abruptly, the field access expression completes abruptly for the same reason.
  - If the value of the *Primary* is `null`, then a `NullPointerException` is thrown.
  - If the field is a non-blank `final`, then the result is the value of the named member field in type T found in the object referenced by the value of the *Primary*.
  - If the field is not `final`, or is a blank `final` and the field access occurs in an instance variable initializer ([§8.3.2](8.3.2.md)), instance initializer ([§8.6](8.6.md)), or constructor ([§8.8](8.8.md)), then the result is a variable, namely the named member field in type T found in the object referenced by the value of the *Primary*.

Note that only the type of the *Primary* expression, not the class of the actual object referred to at run time, is used in determining which field to use.

**Example 15.11.1-1. Static Binding for Field Access**

```
class S           { int x = 0; }
class T extends S { int x = 1; }
class Test1 {
    public static void main(String[] args) {
        T t = new T();
        System.out.println("t.x=" + t.x + when("t", t));
        S s = new S();
        System.out.println("s.x=" + s.x + when("s", s));
        s = t;
        System.out.println("s.x=" + s.x + when("s", s));
    }
    static String when(String name, Object t) {
        return " when " + name + " holds a "
                        + t.getClass() + " at run time.";
    }
}
```

This program produces the output:

```
t.x=1 when t holds a class T at run time.
s.x=0 when s holds a class S at run time.
s.x=0 when s holds a class T at run time.
```

The last line shows that, indeed, the field that is accessed does not depend on the run-time class of the referenced object; even if `s` holds a reference to an object of class `T`, the expression `s.x` refers to the `x` field of class `S`, because the type of the expression `s` is `S`. Objects of class `T` contain two fields named `x`, one for class `T` and one for its superclass `S`.

This lack of dynamic lookup for field accesses allows programs to be run efficiently with straightforward implementations. The power of late binding and overriding is available, but only when instance methods are used. Consider the same example using instance methods to access the fields:

```
class S           { int x = 0; int z() { return x; } }
class T extends S { int x = 1; int z() { return x; } }
class Test2 {
    public static void main(String[] args) {
        T t = new T();
        System.out.println("t.z()=" + t.z() + when("t", t));
        S s = new S();
        System.out.println("s.z()=" + s.z() + when("s", s));
        s = t;
        System.out.println("s.z()=" + s.z() + when("s", s));
    }
    static String when(String name, Object t) {
        return " when " + name + " holds a "
                        + t.getClass() + " at run time.";
    }
}
```

Now the output is:

```
t.z()=1 when t holds a class T at run time.
s.z()=0 when s holds a class S at run time.
s.z()=1 when s holds a class T at run time.
```

The last line shows that, indeed, the method that is accessed *does* depend on the run-time class of the referenced object; when `s` holds a reference to an object of class `T`, the expression `s.z()` refers to the `z` method of class `T`, despite the fact that the type of the expression `s` is `S`. Method `z` of class `T` overrides method `z` of class `S`.

**Example 15.11.1-2. Receiver Variable Is Irrelevant For `static` Field Access**

The following program demonstrates that a null reference may be used to access a class (`static`) variable without causing an exception:

```
class Test3 {
    static String mountain = "Chocorua";
    static Test3 favorite(){
        System.out.print("Mount ");
        return null;
    }
    public static void main(String[] args) {
        System.out.println(favorite().mountain);
    }
}
```

It compiles, executes, and prints:

```
Mount Chocorua
```

Even though the result of `favorite()` is `null`, a `NullPointerException` is not thrown. That "`Mount` " is printed demonstrates that the *Primary* expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field `mountain` is `static`).

## 15.11.2. Accessing Superclass Members using `super`

The form `super.`*Identifier* refers to the field named *Identifier* of the current object, but with the current object viewed as an instance of the superclass of the current class.

The form T`.super.`*Identifier* refers to the field named *Identifier* of the lexically enclosing instance corresponding to T, but with that instance viewed as an instance of the superclass of T.

The forms using the keyword `super` may be used in the locations within a class declaration that allow the keyword `this` as an expression ([§15.8.3](15.8.3.md)).

It is a compile-time error if a field access expression using the keyword `super` occurs in a static context ([§8.1.3](8.1.3.md)) or in an early construction context ([§8.8.7](8.8.7.md)) of the current class.

For a field access expression of the form `super.`*Identifier*:

- It is a compile-time error if the immediately enclosing class or interface declaration of the field access expression is the class `Object` or an interface.

For a field access expression of the form T`.super.`*Identifier*:

- It is a compile-time error if T is the class `Object` or an interface.
- Let U be the immediately enclosing class or interface declaration of the field access expression. It is a compile-time error if U is not an inner class of T or T itself.

Suppose that a field access expression `super.f` appears within class C, and the immediate superclass of C is class S. If `f` in S is accessible from class C ([§6.6](6.6.md)), then `super.f` is treated as if it had been the expression `this.f` in the body of class S. Otherwise, a compile-time error occurs.

Thus, `super.f` can access the field `f` that is accessible in class S, even if that field is hidden by a declaration of a field `f` in class C.

Suppose that a field access expression T`.super.f` appears within class C, and the immediate superclass of the class denoted by T is a class whose fully qualified name is S. If `f` in S is accessible from C, then T`.super.f` is treated as if it had been the expression `this.f` in the body of class S. Otherwise, a compile-time error occurs.

Thus, T`.super.f` can access the field `f` that is accessible in class S, even if that field is hidden by a declaration of a field `f` in class T.

**Example 15.11.2-1. The `super` Expression**

```
interface I           { int x = 0; }
class T1 implements I { int x = 1; }
class T2 extends T1   { int x = 2; }
class T3 extends T2 {
    int x = 3;
    void test() {
        System.out.println("x=\t\t"          + x);
        System.out.println("super.x=\t\t"    + super.x);
        System.out.println("((T2)this).x=\t" + ((T2)this).x);
        System.out.println("((T1)this).x=\t" + ((T1)this).x);
        System.out.println("((I)this).x=\t"  + ((I)this).x);
    }
}
class Test {
    public static void main(String[] args) {
        new T3().test();
    }
}
```

This program produces the output:

```
x=              3
super.x=        2
((T2)this).x=   2
((T1)this).x=   1
((I)this).x=    0
```

Within class `T3`, the expression `super.x` has the same effect as `((T2)this).x` when `x` has package access. Note that `super.x` is not specified in terms of a cast, due to difficulties around access to `protected` members of the superclass.
