JVM Knowledge

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, 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 protected and 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 protected field access or method invocation must be an instance of D or a subclass of D (§4.10.1.8).

  • R is either protected or has default access (that is, neither public nor protected nor private), and is declared by a class in the same run-time package as D.

  • R is private and 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:

  • If R is public, protected, or has default access, then access control throws an IllegalAccessError.
  • If R is private, then the nestmate test failed, and access control fails for the same reason.

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, this nest is a singleton consisting only of the class or interface itself.

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:

    1. The nest host of D, H, is determined (below). If an exception is thrown, then the nestmate test fails for the same reason.
    2. The nest host of C, H', is determined (below). If an exception is thrown, then the nestmate test fails for the same reason.
    3. 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 by throwing an IllegalAccessError.

The nest host of a class or interface M is determined as follows:

  • If M lacks a NestHost attribute, then M is its own nest host.

  • Otherwise, M has a NestHost attribute, and its host_class_index item is used as an index into the run-time constant pool of M. The symbolic reference at that index is resolved to a class or interface H (§5.4.3.1).

    During resolution of this symbolic reference, any of the exceptions pertaining to class or interface resolution can be thrown. Otherwise, resolution of H succeeds.

    If any of the following is true, an IncompatibleClassChangeError is thrown:

    • H is not in the same run-time package as M.
    • H lacks a NestMembers attribute.
    • H has a NestMembers attribute, but there is no entry in its classes array that refers to a class or interface with the name N, where N is the name of M.

    Otherwise, H is the nest host of M.