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

# 15.13. Method Reference Expressions

A method reference expression is used to refer to the invocation of a method without actually performing the invocation. Certain forms of method reference expression also allow class instance creation ([§15.9](15.9.md)) or array creation ([§15.10](15.10.md)) to be treated as if it were a method invocation.

```
MethodReference:
  ExpressionName :: [TypeArguments] Identifier
  Primary :: [TypeArguments] Identifier
  ReferenceType :: [TypeArguments] Identifier
  super :: [TypeArguments] Identifier
  TypeName . super :: [TypeArguments] Identifier
  ClassType :: [TypeArguments] new
  ArrayType :: new
```

If *TypeArguments* is present to the right of `::`, then it is a compile-time error if any of the type arguments are wildcards ([§4.5.1](4.5.1.md)).

If a method reference expression has the form *ExpressionName* `::` *[TypeArguments]* *Identifier* or *Primary* `::` *[TypeArguments]* *Identifier*, it is a compile-time error if the type of the *ExpressionName* or *Primary* is not a reference type.

If a method reference expression has the form `super` `::` *[TypeArguments]* *Identifier*, let T be the type declaration immediately enclosing the method reference expression. It is a compile-time error if T is the class `Object` or T is an interface.

If a method reference expression has the form *TypeName* `.` `super` `::` *[TypeArguments]* *Identifier*, then:

- If *TypeName* denotes a class, C, then it is a compile-time error if C is not a lexically enclosing class of the current class, or if C is the class `Object`.
- If *TypeName* denotes an interface, I, then let T be the type declaration immediately enclosing the method reference expression. It is a compile-time error if I is not a direct superinterface of T, or if there exists some other direct superclass or direct superinterface of T, J, such that J is a subtype of I.
- If *TypeName* denotes a type variable, then a compile-time error occurs.

If a method reference expression has the form `super` `::` *[TypeArguments]* *Identifier* or *TypeName* `.` `super` `::` *[TypeArguments]* *Identifier*, it is a compile-time error if the expression occurs in a static context.

If a method reference expression has the form *ClassType* `::` *[TypeArguments]* `new`, then:

- *ClassType* must denote a class that is accessible ([§6.6](6.6.md)), non-`abstract`, and not an enum type, or a compile-time error occurs.
- If *ClassType* denotes a parameterized type ([§4.5](4.5.md)), then it is a compile-time error if any of its type arguments are wildcards.
- If *ClassType* denotes a raw type ([§4.8](4.8.md)), then it is a compile-time error if *TypeArguments* is present after the `::`.

If a method reference expression has the form *ArrayType* `::` `new`, then *ArrayType* must denote a type that is reifiable ([§4.7](4.7.md)), or a compile-time error occurs.

The target reference of an instance method ([§15.12.4.1](15.12.4.1.md)) may be provided by the method reference expression using an *ExpressionName*, a *Primary*, or `super`, or it may be provided later when the method is invoked. The immediately enclosing instance of a new inner class instance ([§15.9.2](15.9.2.md)) is provided by a lexically enclosing instance of `this` ([§8.1.3](8.1.3.md)).

When more than one member method of a type has the same name, or when a class has more than one constructor, the appropriate method or constructor is selected based on the functional interface type targeted by the method reference expression, as specified in [§15.13.1](15.13.1.md).

If a method or constructor is generic, the appropriate type arguments may either be inferred or provided explicitly. Similarly, the type arguments of a generic type mentioned by the method reference expression may be provided explicitly or inferred.

Method reference expressions are always poly expressions ([§15.2](15.2.md)).

It is a compile-time error if a method reference expression occurs in a program in someplace other than an assignment context ([§5.2](5.2.md)), an invocation context ([§5.3](5.3.md)), or a casting context ([§5.5](5.5.md)).

Evaluation of a method reference expression produces an instance of a functional interface type ([§9.8](9.8.md)). This does *not* cause the execution of the corresponding method; instead, the execution may occur at a later time when an appropriate method of the functional interface is invoked.

Here are some method reference expressions, first with no target reference and then with a target reference:

```
String::length             // instance method
System::currentTimeMillis  // static method
List<String>::size  // explicit type arguments for generic type
List::size          // inferred type arguments for generic type
int[]::clone
T::tvarMember

System.out::println
"abc"::length
foo[x]::bar
(test ? list.replaceAll(String::trim) : list) :: iterator
super::toString
```

Here are some more method reference expressions:

```
String::valueOf       // overload resolution needed
Arrays::sort          // type arguments inferred from context
Arrays::<String>sort  // explicit type arguments
```

Here are some method reference expressions that represent a deferred creation of an object or an array:

```
ArrayList<String>::new     // constructor for parameterized type
ArrayList::new             // inferred type arguments
                           // for generic class
Foo::<Integer>new          // explicit type arguments
                           // for generic constructor
Bar<String>::<Integer>new  // generic class, generic constructor
Outer.Inner::new           // inner class constructor
int[]::new                 // array creation
```

It is not possible to specify a particular signature to be matched, for example, `Arrays::sort(int[])`. Instead, the functional interface provides argument types that are used as input to the overload resolution algorithm ([§15.12.2](15.12.2.md)). This should satisfy the vast majority of use cases; when the rare need arises for more precise control, a lambda expression can be used.

The use of type argument syntax in the class name before a delimiter (`List<String>::size`) raises the parsing problem of distinguishing between `<` as a type argument bracket and `<` as a less-than operator. In theory, this is no worse than allowing type arguments in cast expressions; however, the difference is that the cast case only comes up when a `(` token is encountered; with the addition of method reference expressions, the start of *every* expression is potentially a parameterized type.

**Sections:**

- [15.13.1. Compile-Time Declaration of a Method Reference](15.13.1.md)
- [15.13.2. Type of a Method Reference](15.13.2.md)
- [15.13.3. Run-Time Evaluation of Method References](15.13.3.md)
