To resolve an unresolved symbolic reference from D to a field in a class or interface C, the symbolic reference to C given by the field reference must first be resolved (§5.4.3.1). Therefore, any exception that can be thrown as a result of failure of resolution of a class or interface reference can be thrown as a result of failure of field resolution. If the reference to C can be successfully resolved, an exception relating to the failure of resolution of the field reference itself can be thrown.
When resolving a field reference, field resolution first attempts to look up the referenced field in C and its superclasses:
- If C declares a field with the name and descriptor specified by the field reference, field lookup succeeds. The declared field is the result of the field lookup.
- Otherwise, field lookup is applied recursively to the direct superinterfaces of the specified class or interface C.
- Otherwise, if C has a superclass S, field lookup is applied recursively to S.
- Otherwise, field lookup fails.
Then:
-
If field lookup fails, field resolution throws a
NoSuchFieldError. -
Otherwise, if field lookup succeeds but the referenced field is not accessible (§5.4.4) to D, field resolution throws an
IllegalAccessError. -
Otherwise, let
<E,L_1>be the class or interface in which the referenced field is actually declared and letL_2be the defining loader of D.Given that the type of the referenced field is T_f, let T be T_f if T_f is not an array type, and let T be the element type (§2.4) of T_f otherwise.
The Java Virtual Machine must impose the loading constraint that T^{L_1} = T^{L_2} (§5.3.4).