---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 11 Edition"
java_version: 11
chapter: "Chapter 15. Expressions"
section: "15.13.1"
heading: "15.13.1. Compile-Time Declaration of a Method Reference"
parent: "15.13. Method Reference Expressions"
source: https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.13.1
id: "15.13.1"
url: https://jvm.obfuscated.software/jls/se11/15.13.1.html
raw: https://jvm.obfuscated.software/jls/se11/15.13.1.md
---

# 15.13.1. Compile-Time Declaration of a Method Reference

The *compile-time declaration* of a method reference expression is the method to which the expression refers. In special cases, the compile-time declaration does not actually exist, but is a notional method that represents a class instance creation or an array creation. The choice of compile-time declaration depends on a function type targeted by the expression, just as the compile-time declaration of a method invocation depends on the invocation's arguments ([§15.12.3](15.12.3.md)).

The search for a compile-time declaration mirrors the process for method invocations in [§15.12.1](15.12.1.md) and [§15.12.2](15.12.2.md), as follows:

- First, a type to search is determined:

  - If the method reference expression has the form *ExpressionName* `::` *[TypeArguments]* *Identifier* or *Primary* `::` *[TypeArguments]* *Identifier*, the type to search is the type of the expression preceding the `::` token.
  - If the method reference expression has the form *ReferenceType* `::` *[TypeArguments]* *Identifier*, the type to search is the result of capture conversion ([§5.1.10](5.1.10.md)) applied to *ReferenceType*.
  - If the method reference expression has the form `super` `::` *[TypeArguments]* *Identifier*, the type to search is the superclass type of the class whose declaration contains the method reference.
  - If the method reference expression has the form *TypeName* `.` `super` `::` *[TypeArguments]* *Identifier*, then if *TypeName* denotes a class, the type to search is the superclass type of the named class; otherwise, *TypeName* denotes an interface, and the corresponding superinterface type of the class or interface whose declaration contains the method reference is the type to search.
  - For the two other forms (involving `::` `new`), the referenced method is notional and there is no type to search.

- Second, given a targeted function type with *n* parameters, a set of potentially applicable methods is identified:

  - If the method reference expression has the form *ReferenceType* `::` *[TypeArguments]* *Identifier*, then the potentially applicable methods are:

    - the member methods of the type to search that would be potentially applicable ([§15.12.2.1](15.12.2.1.md)) for a method invocation which names *Identifier*, has arity *n*, has type arguments *TypeArguments*, and appears in the same class as the method reference expression; plus
    - the member methods of the type to search that would be potentially applicable for a method invocation which names *Identifier*, has arity *n*-1, has type arguments *TypeArguments*, and appears in the same class as the method reference expression.

    Two different arities, *n* and *n*-1, are considered, to account for the possibility that this form refers to either a `static` method or an instance method.

  - If the method reference expression has the form *ClassType* `::` *[TypeArguments]* `new`, then the potentially applicable methods are a set of notional methods corresponding to the constructors of *ClassType*.

    If *ClassType* is a raw type, but is not a non-`static` member type of a raw type, the candidate notional member methods are those specified in [§15.9.3](15.9.3.md) for a class instance creation expression that uses `<>` to elide the type arguments to a class. Otherwise, the candidate notional member methods are the constructors of *ClassType*, treated as if they were methods with return type *ClassType*.

    Among these candidates, the potentially applicable methods are the notional methods that would be potentially applicable for a method invocation which has arity *n*, has type arguments *TypeArguments*, and appears in the same class as the method reference expression.

  - If the method reference expression has the form *ArrayType* `::` `new`, a single notional method is considered. The method has a single parameter of type `int`, returns the *ArrayType*, and has no `throws` clause. If *n* = 1, this is the only potentially applicable method; otherwise, there are no potentially applicable methods.

  - For all other forms, the potentially applicable methods are the member methods of the type to search that would be potentially applicable for a method invocation which names *Identifier*, has arity *n*, has type argument *TypeArguments*, and appears in the same class as the method reference expression.

- Finally, if there are no potentially applicable methods, then there is no compile-time declaration.

  Otherwise, given a targeted function type with parameter types P_1, ..., P_n and a set of potentially applicable methods, the compile-time declaration is selected as follows:

  - If the method reference expression has the form *ReferenceType* `::` *[TypeArguments]* *Identifier*, then two searches for a most specific applicable method are performed. Each search is as specified in [§15.12.2.2](15.12.2.2.md) through [§15.12.2.5](15.12.2.5.md), with the clarifications below. Each search produces a set of applicable methods and, possibly, designates a most specific method of the set. In the case of an error as specified in [§15.12.2.4](15.12.2.4.md), the set of applicable methods is empty. In the case of an error as specified in [§15.12.2.5](15.12.2.5.md), there is no most specific method.

    In the first search, the method reference is treated as if it were an invocation with argument expressions of types P_1, ..., P_n. Type arguments, if any, are given by the method reference expression.

    In the second search, if P_1, ..., P_n is not empty and P_1 is a subtype of *ReferenceType*, then the method reference expression is treated as if it were a method invocation expression with argument expressions of types P_2, ..., P_n. If *ReferenceType* is a raw type, and there exists a parameterization of this type, G`<`...`>`, that is a supertype of P_1, the type to search is the result of capture conversion ([§5.1.10](5.1.10.md)) applied to G`<`...`>`; otherwise, the type to search is the same as the type of the first search. Type arguments, if any, are given by the method reference expression.

    If the first search produces a most specific method that is `static`, and the set of applicable methods produced by the second search contains no non-`static` methods, then the compile-time declaration is the most specified method of the first search.

    Otherwise, if the set of applicable methods produced by the first search contains no `static` methods, and the second search produces a most specific method that is non-`static`, then the compile-time declaration is the most specific method of the second search.

    Otherwise, there is no compile-time declaration.

  - For all other forms of method reference expression, one search for a most specific applicable method is performed. The search is as specified in [§15.12.2.2](15.12.2.2.md) through [§15.12.2.5](15.12.2.5.md), with the clarifications below.

    The method reference is treated as if it were an invocation with argument expressions of types P_1, ..., P_n; the type arguments, if any, are given by the method reference expression.

    If the search results in an error as specified in [§15.12.2.2](15.12.2.2.md) through [§15.12.2.5](15.12.2.5.md), or if the most specific applicable method is `static`, there is no compile-time declaration.

    Otherwise, the compile-time declaration is the most specific applicable method.

It is a compile-time error if a method reference expression has the form *ReferenceType* `::` *[TypeArguments]* *Identifier*, and the compile-time declaration is `static`, and *ReferenceType* is not a simple or qualified name ([§6.2](6.2.md)).

It is a compile-time error if the method reference expression has the form `super` `::` *[TypeArguments]* *Identifier* or *TypeName* `.` `super` `::` *[TypeArguments]* *Identifier*, and the compile-time declaration is `abstract`.

It is a compile-time error if the method reference expression has the form *TypeName* `.` `super` `::` *[TypeArguments]* *Identifier*, and *TypeName* denotes an interface, and there exists a method, distinct from the compile-time declaration, that overrides ([§8.4.8](8.4.8.md), [§9.4.1](9.4.1.md)) the compile-time declaration from a direct superclass or direct superinterface of the type whose declaration immediately encloses the method reference expression.

It is a compile-time error if the method reference expression is of the form *ClassType* `::` *[TypeArguments]* `new` and a compile-time error would occur when determining an enclosing instance for *ClassType* as specified in [§15.9.2](15.9.2.md) (treating the method reference expression as if it were an unqualified class instance creation expression).

A method reference expression of the form *ReferenceType* `::` *[TypeArguments]* *Identifier* can be interpreted in different ways. If *Identifier* refers to an instance method, then the implicit lambda expression has an extra parameter compared to if *Identifier* refers to a `static` method. It is possible for *ReferenceType* to have both kinds of applicable methods, so the search algorithm described above identifies them separately, since there are different parameter types for each case.

An example of ambiguity is:

```
interface Fun<T,R> { R apply(T arg); }

class C {
    int size() { return 0; }
    static int size(Object arg) { return 0; }

    void test() {
        Fun<C, Integer> f1 = C::size;
          // Error: instance method size()
          // or static method size(Object)?
    }
}
```

This ambiguity cannot be resolved by providing an applicable instance method which is more specific than an applicable `static` method:

```
interface Fun<T,R> { R apply(T arg); }

class C {
    int size() { return 0; }
    static int size(Object arg) { return 0; }
    int size(C arg) { return 0; }

    void test() {
        Fun<C, Integer> f1 = C::size;
          // Error: instance method size()
          // or static method size(Object)?
    }
}
```

The search is smart enough to ignore ambiguities in which all the applicable methods (from both searches) are instance methods:

```
interface Fun<T,R> { R apply(T arg); }

class C {
    int size() { return 0; }
    int size(Object arg) { return 0; }
    int size(C arg) { return 0; }

    void test() {
        Fun<C, Integer> f1 = C::size;
          // OK: reference is to instance method size()
    }
}
```

For convenience, when the name of a generic type is used to refer to an instance method (where the receiver becomes the first parameter), the target type is used to determine the type arguments. This facilitates usage like `Pair::first` in place of `Pair<String,Integer>::first`. Similarly, a method reference like `Pair::new` is treated like a "diamond" instance creation (`new Pair<>()`). Because the "diamond" is implicit, this form does *not* instantiate a raw type; in fact, there is no way to express a reference to the constructor of a raw type.

For some method reference expressions, there is only one possible compile-time declaration with only one possible invocation type ([§15.12.2.6](15.12.2.6.md)), regardless of the targeted function type. Such method reference expressions are said to be *exact*. A method reference expression that is not exact is said to be *inexact*.

A method reference expression ending with *Identifier* is exact if it satisfies all of the following:

- If the method reference expression has the form *ReferenceType* `::` *[TypeArguments]* *Identifier*, then *ReferenceType* does not denote a raw type.
- The type to search has exactly one member method with the name *Identifier* that is accessible to the class or interface in which the method reference expression appears.
- This method is not variable arity ([§8.4.1](8.4.1.md)).
- If this method is generic ([§8.4.4](8.4.4.md)), then the method reference expression provides *TypeArguments*.

A method reference expression of the form *ClassType* `::` *[TypeArguments]* `new` is exact if it satisfies all of the following:

- The type denoted by *ClassType* is not raw, or is a non-`static` member type of a raw type.
- The type denoted by *ClassType* has exactly one constructor that is accessible to the class or interface in which the method reference expression appears.
- This constructor is not variable arity.
- If this constructor is generic, then the method reference expression provides *TypeArguments*.

A method reference expression of the form *ArrayType* `::` `new` is always exact.
