---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 19 Edition"
java_version: 19
chapter: "Chapter 5. Loading, Linking, and Initializing"
section: "5.4.4"
heading: "5.4.4. Access Control"
parent: "5.4. Linking"
source: https://docs.oracle.com/javase/specs/jvms/se19/html/jvms-5.html#jvms-5.4.4
id: "5.4.4"
url: https://jvm.obfuscated.software/jvms/se19/5.4.4.html
raw: https://jvm.obfuscated.software/jvms/se19/5.4.4.md
---

# 5.4.4. Access Control

Access control is applied during resolution ([§5.4.3](5.4.3.md)) 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](5.3.6.md)).
- 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 `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](4.10.1.8.md)).

- 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 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](4.7.29.md)). Each of them in turn designates it as the nest host, using the `NestHost` attribute ([§4.7.28](4.7.28.md)). 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:

  1. 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.
  2. 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'.
  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.

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 ([§5.4.3.1](5.4.3.1.md)).

  If resolution of the symbolic reference fails, then `M` is 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 `M` is determined by the following rules:

  - If any of the following is true, then `M` is its own nest host:

    - 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`.
