Access control is applied during resolution (§5.4.3) to ensure that a reference to a class, interface, field, or method is permitted. Access control succeeds if a specified class, interface, field, or method is accessible to the referring class or interface.
A class or interface C is accessible to a class or interface D if and only if one of the following is true:
- C is
public, and a member of the same run-time module as D (§5.3.6). - C is
public, and a member of a different run-time module than D, and C's run-time module is read by D's run-time module, and C's run-time module exports C's run-time package to D's run-time module. - C is not
public, and C and D are members of the same run-time package.
If C is not accessible to D, then access control throws an IllegalAccessError. Otherwise, access control succeeds.
A field or method R is accessible to a class or interface D if and only if any of the following is true:
-
R is
public. -
R is
protectedand is declared in a class C, and D is either a subclass of C or C itself.Furthermore, if R is not
static, then the symbolic reference to R must contain a symbolic reference to a class T, such that T is either a subclass of D, a superclass of D, or D itself.During verification of D, it was required that, even if T is a superclass of D, the target reference of a
protectedfield access or method invocation must be an instance of D or a subclass of D (§4.10.1.8). -
R is either
protectedor has default access (that is, neitherpublicnorprotectednorprivate), and is declared by a class in the same run-time package as D. -
R is
privateand is declared by a class or interface C that belongs to the same nest as D, according to the nestmate test below.
If R is not accessible to D, then access control throws an IllegalAccessError. Otherwise, access control succeeds.
A nest is a set of classes and interfaces that allow mutual access to their private members. One of the classes or interfaces is the nest host. It enumerates the classes and interfaces which belong to the nest, using the NestMembers attribute (§4.7.29). Each of them in turn designates it as the nest host, using the NestHost attribute (§4.7.28). A class or interface which lacks a NestHost attribute belongs to the nest hosted by itself; if it also lacks a NestMembers attribute, then this nest is a singleton consisting only of the class or interface itself.
The Java Virtual Machine determines the nest to which a given class or interface belongs (that is, the nest host designated by the class or interface) as part of access control, rather than when the class or interface is loaded. Certain methods of the Java SE Platform API may determine the nest to which a given class or interface belongs prior to access control, in which case the Java Virtual Machine respects that prior determination during access control.
To determine whether a class or interface C belongs to the same nest as a class or interface D, the nestmate test is applied. C and D belong to the same nest if and only if the nestmate test succeeds. The nestmate test is as follows:
-
If C and D are the same class or interface, then the nestmate test succeeds.
-
Otherwise, the following steps are performed, in order:
- Let H be the nest host of D, if the nest host of D has previously been determined. If the nest host of D has not previously been determined, then it is determined using the algorithm below, yielding H.
- Let H' be the nest host of C, if the nest host of C has previously been determined. If the nest host of C has not previously been determined, then it is determined using the algorithm below, yielding H'.
- H and H' are compared. If H and H' are the same class or interface, then the nestmate test succeeds. Otherwise, the nestmate test fails.
The nest host of a class or interface M is determined as follows:
-
If
Mlacks aNestHostattribute, thenMis its own nest host. -
Otherwise,
Mhas aNestHostattribute, and itshost_class_indexitem is used as an index into the run-time constant pool ofM. The symbolic reference at that index is resolved (§5.4.3.1).If resolution of the symbolic reference fails, then
Mis its own nest host. Any exception thrown as a result of failure of class or interface resolution is not rethrown.Otherwise, resolution of the symbolic reference succeeds. Let H be the resolved class or interface. The nest host of
Mis determined by the following rules:-
If any of the following is true, then
Mis its own nest host:- H is not in the same run-time package as
M. - H lacks a
NestMembersattribute. - H has a
NestMembersattribute, but there is no entry in itsclassesarray that refers to a class or interface with the nameN, whereNis the name ofM.
- H is not in the same run-time package as
-
Otherwise, H is the nest host of
M.
-