Java 8 method reference example

In Java 8, we can refer a method from class or object using class::methodName type syntax. Let’s learn about different types of available method references in java 8.

Table of Contents

1. Types of Method References
2. Reference to static method - Class::staticMethodName
3. Reference to instance method from instance - ClassInstance::instanceMethodName
4. Reference to instance method from class type - Class::instanceMethodName
5. Reference to constructor - Class::new

1. Types of method references

Java 8 allows four types of method references.

Method ReferenceDescriptionMethod reference example
Reference to static methodUsed to refer static methods from a classMath::max equivalent to Math.max(x,y)
Reference to instance method from instanceRefer to an instance method using a reference to the supplied objectSystem.out::println equivalent to System.out.println(x)
Reference to instance method from class typeInvoke the instance method on a reference to an object supplied by the contextString::length equivalent to str.length()
Reference to constructorReference to a constructorArrayList::new equivalent to new ArrayList()

2. Method reference to static method – Class::staticMethodName

An example to use Math.max() which is static method.

List<Integer> integers = Arrays.asList(1,12,433,5);
		
Optional<Integer> max = integers.stream().reduce( Math::max ); 

max.ifPresent(value -> System.out.println(value)); 

Output:

433

3. Method reference to instance method from instance – ClassInstance::instanceMethodName

In above example, we use System.out.println(value) to print the max value found. We can use System.out::println to print the value.

List<Integer> integers = Arrays.asList(1,12,433,5);
		
Optional<Integer> max = integers.stream().reduce( Math::max ); 

max.ifPresent( System.out::println ); 

Output:

433

4. Method reference to instance method from class type – Class::instanceMethodName

In this example, s1.compareTo(s2) is referred as String::compareTo.

List<String> strings = Arrays
		.asList("how", "to", "do", "in", "java", "dot", "com");

List<String> sorted = strings
		.stream()
		.sorted((s1, s2) -> s1.compareTo(s2))
		.collect(Collectors.toList());

System.out.println(sorted);

List<String> sortedAlt = strings
		.stream()
		.sorted(String::compareTo)
		.collect(Collectors.toList());

System.out.println(sortedAlt);

Output:

[com, do, dot, how, in, java, to]
[com, do, dot, how, in, java, to]

5. Reference to constructor – Class::new

The first method can be updated to create a list of integers from 1 to 100. Using lambda expression is rather easy. To create a new instance of ArrayList, we have use ArrayList::new.

List<Integer> integers = IntStream
				.range(1, 100)
				.boxed()
				.collect(Collectors.toCollection( ArrayList::new ));

Optional<Integer> max = integers.stream().reduce(Math::max); 

max.ifPresent(System.out::println); 

Output:

99

That’s 4 type of method references in java 8 lambda enhancements.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

6 thoughts on “Java 8 method reference example”

  1. ArrayList::new equivalent to new ArrayList() this is not correct this applicable only if “The target type of this expression must be a functional interface”

  2. How do you come to know about the parameters that are taken by these static methods.

    For Example:
    If I want to write System.out.println(“Hello”);
    How do i write it using method reference ?

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.