Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java 8 Method Reference (with Examples)

Since Java 8, in simplest words, the method references are a way to refer to methods or constructors without invoking them. Learn the syntax with examples.

Lokesh Gupta

May 29, 2024

Java 8
Java 8, Lambda Expression
Java 8 Method References

Since Java 8, in simplest words, the method references are a way to refer to methods or constructors without invoking them. They are a shorthand notation of a lambda expression and can be used anywhere a functional interface is expected.

The method references are denoted using ‘Class::methodName‘ type syntax. Let’s learn about different types of available method references in Java 8.

1. Types of Method References

Java 8 allows four types of method references.

Method Reference ToDescriptionSyntax
Static methodUsed to refer to static methods from a classClassName::staticMethodName
Instance method from a specific instanceRefer to an instance method using a reference to the supplied objectinstance::instanceMethodName
Instance method from the Class typeInvoke the instance method on a reference to an object supplied by the contextClassName::instanceMethodName
ConstructorReference to a constructorClassName::new

Please note that the method references always utilize the :: operator. Let’s explore each type with examples.

2. Method Reference to a Static Method

This method reference is used when we want to refer to a static method without invoking it. An example to use Math.max() which is a 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 of a Particular Object

This type of method reference refers to an instance method of a specific object. It is used when we want to call a method on a particular instance.

Consider the following PrintService class.

public class PrintService {

  public void print(String message) {
      System.out.println(message);
  }
}

We can invoke the print() from an instance of PrintService using the ‘printService::print‘ method reference.

PrintService printService = new PrintService();
List<String> messages = Arrays.asList("Hello", "World", "Method", "References");

// using lambda expression
messages.forEach(message -> printService.print(message));

// iusing method reference
messages.forEach(printService::print);

The method reference is much cleaner and more readable, as the code clearly shows our intention.

4. Method Reference to Instance Method from a Class Type

In this method reference, we do not create any class instance. We can directly use the print() method from the class type PrintService.

List<String> messages = Arrays.asList("Hello", "World", "Method", "References");

messages.forEach(PrintService::print);

5. Method Reference to Constructor

This type of method reference refers to a constructor. It’s used when we want to create a new instance of a class.

For example, to create a new instance of ArrayList, we have use ArrayList::new.

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

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

Happy Learning !!

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Table of Contents

  • 1. Types of Method References
  • 2. Method Reference to a Static Method
  • 3. Method Reference to Instance Method of a Particular Object
  • 4. Method Reference to Instance Method from a Class Type
  • 5. Method Reference to Constructor
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Convert Byte[] to String and Vice-versa

Next

Java remove non-printable non-ascii characters using regex

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap