---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 12 Edition"
java_version: 12
chapter: "Chapter 15. Expressions"
section: "15.12.4.4"
heading: "15.12.4.4. Locate Method to Invoke"
parent: "15.12.4. Run-Time Evaluation of Method Invocation"
source: https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.12.4.4
id: "15.12.4.4"
url: https://jvm.obfuscated.software/jls/se12/15.12.4.4.html
raw: https://jvm.obfuscated.software/jls/se12/15.12.4.4.md
---

# 15.12.4.4. Locate Method to Invoke

As in the previous section ([§15.12.4.3](15.12.4.3.md)):

- Let T be the qualifying type of the method invocation ([§13.1](13.1.md)).
- Let `m` be the method found in T or a supertype of T. (Note that `m` was merely the name of the method in the previous section; here it is the actual declaration.)
- Let C be the class or interface that declares `m`.

The strategy for locating a method to invoke depends on the invocation mode:

- If the invocation mode is `static`, no target reference is needed and overriding is not allowed. Method `m` of class or interface C is the one to be invoked.

- Otherwise, an instance method is to be invoked and there is a target reference. If the target reference is `null`, a `NullPointerException` is thrown at this point. Otherwise, the target reference is said to refer to a *target object* and will be used as the value of the keyword `this` in the invoked method. The other three possibilities for the invocation mode are then considered:

  - If the invocation mode is `super`, overriding is not allowed. Method `m` of class or interface C is the one to be invoked. If `m` is `abstract`, an `AbstractMethodError` is thrown.

  - Otherwise, if the invocation mode is `virtual`, and T and `m` jointly indicate a signature polymorphic method ([§15.12.3](15.12.3.md)), then the target object is an instance of `java.lang.invoke.MethodHandle` or `java.lang.invoke.VarHandle`. The target object encapsulates state which is matched against the information associated with the method invocation at compile time. Details of this matching are given in *The Java Virtual Machine Specification, Java SE 12 Edition* and the Java SE Platform API. If matching succeeds, then either the method referenced by the `java.lang.invoke.MethodHandle` instance is directly and immediately invoked, or the variable represented by the `java.lang.invoke.VarHandle` instance is directly and immediately accessed, *and in either case the procedure in [§15.12.4.5](15.12.4.5.md) is not executed*. If matching fails, then a `java.lang.invoke.WrongMethodTypeException` is thrown.

  - Otherwise, the invocation mode is `interface` or `virtual`.

    If the method `m` of class or interface C is `private`, then it is the method to be invoked.

    Otherwise, overriding may occur. A *dynamic method lookup*, specified below, is used to locate the method to invoke. The lookup procedure starts from class R, the actual run-time class of the target object.

    Note that for invocation mode `interface`, R necessarily implements T; for invocation mode `virtual`, R is necessarily either T or a subclass of T. If the target object is an array, then R is a "class" representing an array type.

The procedure for dynamic method lookup is as follows. Let S be the class to search, beginning with R. Then:

1. If class S contains a declaration for a method that overrides method `m` of class or interface C from R ([§8.4.8.1](8.4.8.1.md)), then that overriding method is the method to be invoked, and the procedure terminates.

2. Otherwise, if S has a superclass, then steps 1 and 2 of this lookup procedure are performed recursively using the direct superclass of S in place of S; the method to be invoked, if any, is the result of the recursive invocation of this lookup procedure.

3. If no method is found by the previous two steps, the superinterfaces of S are searched for a suitable method.

   A set of candidate methods is considered with the following properties: (i) each method is declared in a (direct or indirect) superinterface of R; (ii) each method has the name and descriptor required by the method invocation; (iii) each method is non-`static` and non-`private`; (iv) for each method, where the method's declaring interface is I, there is no other method satisfying (i) through (iii) that is declared in a subinterface of I.

   If this set contains a default method, one such method is the method to be invoked. Otherwise, an `abstract` method in the set is selected as the method to be invoked.

Dynamic method lookup may cause the following errors to occur:

- If the method to be invoked is `abstract`, an `AbstractMethodError` is thrown.
- If the method to be invoked is `default`, and more than one default method appears in the set of candidates in step 3 above, an `IncompatibleClassChangeError` is thrown.
- If the invocation mode is `interface` and the method to be invoked is neither `public` nor `private`, an `IllegalAccessError` is thrown.

The above procedure (if it terminates without error) will find a non-`abstract`, accessible method to invoke, provided that all classes and interfaces in the program have been consistently compiled. However, if this is not the case, then various errors may occur, as specified above; additional details about the behavior of the Java Virtual Machine under these circumstances are given by *The Java Virtual Machine Specification, Java SE 12 Edition*.

The dynamic lookup process, while described here explicitly, will often be implemented implicitly, for example as a side-effect of the construction and use of per-class method dispatch tables, or the construction of other per-class structures used for efficient dispatch.

**Example 15.12.4.4-1. Overriding and Method Invocation**

```
class Point {
    final int EDGE = 20;
    int x, y;
    void move(int dx, int dy) {
        x += dx; y += dy;
        if (Math.abs(x) >= EDGE || Math.abs(y) >= EDGE)
            clear();
    }
    void clear() {
        System.out.println("\tPoint clear");
        x = 0; y = 0;
    }
}
class ColoredPoint extends Point {
    int color;
    void clear() {
        System.out.println("\tColoredPoint clear");
        super.clear();
        color = 0;
    }
}
```

Here, the subclass `ColoredPoint` extends the `clear` abstraction defined by its superclass `Point`. It does so by overriding the `clear` method with its own method, which invokes the `clear` method of its superclass, using the form `super.clear()`.

This method is then invoked whenever the target object for an invocation of `clear` is a `ColoredPoint`. Even the method `move` in `Point` invokes the `clear` method of class `ColoredPoint` when the class of `this` is `ColoredPoint`, as shown by the output of this test program:

```
class Test1 {
    public static void main(String[] args) {
        Point p = new Point();
        System.out.println("p.move(20,20):");
        p.move(20, 20);

        ColoredPoint cp = new ColoredPoint();
        System.out.println("cp.move(20,20):");
        cp.move(20, 20);

        p = new ColoredPoint();
        System.out.println("p.move(20,20), p colored:");
        p.move(20, 20);
    }
}
```

which is:

```
p.move(20,20):
        Point clear
cp.move(20,20):
        ColoredPoint clear
        Point clear
p.move(20,20), p colored:
        ColoredPoint clear
        Point clear
```

Overriding is sometimes called "late-bound self-reference"; in this example it means that the reference to `clear` in the body of `Point.move` (which is really syntactic shorthand for `this.clear`) invokes a method chosen "late" (at run time, based on the run-time class of the object referenced by `this`) rather than a method chosen "early" (at compile time, based only on the type of `this`). This provides the programmer a powerful way of extending abstractions and is a key idea in object-oriented programming.

**Example 15.12.4.4-2. Method Invocation Using `super`**

An overridden instance method of a superclass may be accessed by using the keyword `super` to access the members of the immediate superclass, bypassing any overriding declaration in the class that contains the method invocation.

When accessing an instance variable, `super` means the same as a cast of `this` ([§15.11.2](15.11.2.md)), but this equivalence does not hold true for method invocation. This is demonstrated by the example:

```
class T1 {
    String s() { return "1"; }
}
class T2 extends T1 {
    String s() { return "2"; }
}
class T3 extends T2 {
    String s() { return "3"; }
    void test() {
        System.out.println("s()=\t\t"          + s());
        System.out.println("super.s()=\t"      + super.s());
        System.out.println("((T2)this).s()=\t" + ((T2)this).s());
        System.out.println("((T1)this).s()=\t" + ((T1)this).s());
    }
}
class Test2 {
    public static void main(String[] args) {
        T3 t3 = new T3();
        t3.test();
    }
}
```

which produces the output:

```
s()=            3
super.s()=      2
((T2)this).s()= 3
((T1)this).s()= 3
```

The casts to types `T1` and `T2` do not change the method that is invoked, because the instance method to be invoked is chosen according to the run-time class of the object referred to by `this`. A cast does not change the class of an object; it only checks that the class is compatible with the specified type.
