Java 8: Method References

DZone 's Guide to

Java 8: Method References

Want to learn more about using different types of method references in Java? Check out this post to learn more about this special form of the lambda expression.

· Java Zone ·
Free Resource

This article is in continuation of my previous posts on functional interfaces, static and default methods, and lambda expressions.

Method references are a special form of the lambda expression. Since your lambda expressions are doing nothing other than invoking existing behavior (methods), you can achieve the same result by referring to it by name.

  • :: is used to refer to a method.
  • Method type arguments are inferred by JRE at runtime from the context it is defined.

Types of Method References

  • Static method reference
  • Instance method reference of a particular object
  • Instance method reference of an arbitrary object of a particular type
  • Constructor reference

Static Method Reference

When you refer to the static method of Containing a class, e.g. ClassName::someStaticMethodName

class MethodReferenceExample {
  public static int compareByAge(Employee first, Employee second) {
    return Integer.compare(first.age, second.age);

Comparator compareByAge = MethodReferenceExample::compareByAge;

Instance Method Reference of a Particular Object

When you refer to the instance method of the particular object, you will use the containingObjectReference::someInstanceMethodName

static class MyComparator {
  public int compareByFirstName(User first, User second) {
    return first.getFirstName().compareTo(second.getFirstName());

  public int compareByLastName(User first, User second) {
    return first.getLastName().compareTo(second.getLastName());

private static void instanceMethodReference() {
  System.err.println("Instance method reference");
  List<User> users = Arrays.asList(new User("Gaurav", "Mazra"),
      new User("Arnav", "Singh"), new User("Daniel", "Verma"));
  MyComparator comparator = new MyComparator();
  Collections.sort(users, comparator::compareByFirstName);

Instance Method Reference of an Arbitrary Object 

When you refer to the instance method of a class with the ClassName , you will get the instance method reference of an arbitrary object of a particular type, such asClassName::someInstanceMethod;

Comparator<String> stringIgnoreCase = String::compareToIgnoreCase;
//this is equivalent to
Comparator<String> stringComparator = (first, second) -> first.compareToIgnoreCase(second);

Constructor Reference

When you refer to a constructor of a class in lambda, you will get a constructor reference, such as ClassName::new.

Function<String, Job> jobCreator = Job::new;
//the above function is equivalent to
Function<String, Job> jobCreator2 = (jobName) -> return new Job(jobName);

You can find the full example on GitHub.

constructor method, instance method, java, java 8, java 8 lambda expressions, lambda, method references, objects, static method, tutorial

Published at DZone with permission of Gaurav Rai Mazra , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}