JVM Knowledge

Let C be the class containing the method invocation, and let T be the qualifying type of the method invocation (§13.1), and let m be the name of the method as determined at compile time (§15.12.3).

An implementation of the Java programming language must ensure, as part of linkage, that the method m still exists in the type T. If this is not true, then a NoSuchMethodError (which is a subclass of IncompatibleClassChangeError) occurs.

If the invocation mode is interface, then the implementation must also check that the target reference type still implements the specified interface. If the target reference type does not still implement the interface, then an IncompatibleClassChangeError occurs.

The implementation must also ensure, during linkage, that the type T and the method m are accessible:

  • For the type T:

    • If T is in the same package as C, then T is accessible.
    • If T is in a different package than C, and T is public, then T is accessible.
    • If T is in a different package than C, and T is protected, then T is accessible if and only if C is a subclass of T.
  • For the method m:

    • If m is public, then m is accessible. (All members of interfaces are public (§9.2).)
    • If m is protected, then m is accessible if and only if either T is in the same package as C, or C is T or a subclass of T.
    • If m has package access, then m is accessible if and only if T is in the same package as C.
    • If m is private, then m is accessible if and only if C is T, or C encloses T, or T encloses C, or T and C are both enclosed by a third class.

If either T or m is not accessible, then an IllegalAccessError occurs (§12.3).