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 member fields of type T, then the field access is ambiguous and a compile-time error occurs.
- If the identifier does not name an accessible member field of type T, then the field access is undefined and a compile-time error occurs.
- Otherwise, the identifier names a single accessible member field of type T and the type of the field access expression is the declared type of the field. At run time, the result of the field access expression is computed as follows:
- If the field is static:
- If the field is final, 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, 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:
- If the value of the Primary is null, then a
NullPointerException is thrown.
- If the field is final, then the result is the value of the specified instance variable in the object referenced by the value of the Primary.
- If the field is not final, then the result is a variable, namely, the specified instance variable in the object referenced by the value of the Primary.
Note, specifically, 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.