Skip to content

Kinds Of Method References

In this lesson, you will be introduced to different kinds of method references and their syntaxes.

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

Introduction

Method references are commonly used in functional programming, especially when working with streams, to specify a method that should be used as a parameter in a method call.

Method references in Java allow passing methods around as if they were values. Rather than passing a method as an object, you can use method references to pass the method itself. This can make code more concise and easier to read, especially when working with functional interfaces and lambdas.

Syntax (generic)

The generic syntax for all these method references is as follows.

ClassName/ObjectName::MethodName

What are the different kinds of method references?

In the previous lesson, you learned what method references are and how to use them. Let us take a step further and learn how to use them in different scenarios.

The four kinds of method references in Java are based on the type of method referenced and what it is called.

Here's a brief explanation of each kind:

A) Reference to static methods

A static method reference refers to a static method of a class. This is done using the class name followed by the method name, separated by a double colon :: operator.

For example, Math::abs refers to the static abs method of the Math class.

B) Reference to instance methods of particular objects

An instance method reference refers to a non-static method of an object. This is done using the object name followed by the method name, separated by the double colon :: operator.

For example, str::length refers to the length method of the str object.

C) Reference to an instance method of a class

This is also called "Reference to an instance method of an arbitrary object of a particular type".

An instance method reference is also used to refer to a non-static method of a class. This is done using the class name followed by the method name, separated by a double colon :: operator.

The method reference must be used in a context where an object of that class is provided as an argument. For example, String::compareToIgnoreCase refers to the compareToIgnoreCase method of the String class.

D) Reference to a constructor.

A constructor method reference refers to a constructor of a class. This is done using the class name followed by the new keyword, separated by a double colon :: operator.

For example, ArrayList::new refers to the constructor of the ArrayList class.

In the next lessons, you will learn each of them in detail.

Java Streams APIJava

Gopi Gorantala Twitter

Gopi is a software engineer with over 14 years of experience. He specializes in Java-based technology stack and has worked for various startups, the European government, and technology giants.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.