This article will take you through the four types of method references in Java with examples.
1. Static Method Reference
The first type of method reference is referencing a static method.
Definition
Imagine you have a lambda expression like this:
(args) -> Class.staticMethod(args)
This can be transformed into a method reference:
Class::staticMethod
When this happens, the args are automatically passed through the reference without having to write it down manually!
Example
The following class represents a Person, and as you can see, it contains a static method - as a reminder, a static method can be called without having to create an instance of the class first.
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public static void staticMethodReference(Person person) {
System.out.println("Static method reference: " + person.getName());
}
public String getName() {
return name;
}
}
As you can see, the staticMethodReference method contains the keyword static. Now, after creating a list of people, you could write a method reference for this static method:
public class Main {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("Susan"),
new Person("Kenny"),
);
// Reference to a static method
persons.forEach(Person::staticMethodReference);
}
}
This method reference is passed into the forEach method, and the result will print the name of each element in the persons list.
There you have it! Now, take a look at a reference to an instance method.
2. Instance Method Reference of a Particular Object
There are two ways to reference an instance method, and here you'll look at the first one.
Definition
The first method reference, for instance, methods, is when the instance method is referencing a particular object.
(obj, args) -> obj.instanceMethod(args);
This can be transformed into a method reference:
object::instanceMethod
Just like with the static method, the args are automatically passed through the reference without having to write it down manually.
Example
To create a reference to an instance method, you need an instance method! So you can edit the Person class to now contain only an instance method:
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void instanceMethodReference(Person person) {
System.out.println("Instance method reference: " + person.name);
}
}
As you can see, this method requires an instance of Person to already exist since it calls on the instance variable name, and it doesn't have the word static! Now you can create a method reference to this:
public class Main {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("Susan"),
new Person("Kenny")
);
// Reference an instance method of an object
Person person = new Person("Maria");
persons.forEach(person::instanceMethodReference);
}
}
As you can see, the method reference takes the person object that was created and references it to the instanceMethodReference method. The result of this reference would print the name of each persons.
Instance method reference: Susan
Instance method reference: Kenny
3. Instance Method Reference of an arbitrary Object
This is the second way to reference an instance method.
Definition
With this method reference, you reference a method of a particular type that can be applied to any (compatible) object.
(args) -> obj.instanceMethod(args);
This can be transformed into a method reference:
Class::instanceMethod
Again, the args are automatically passed through the reference without having to write it down manually. While this syntax uses the term obj, it is important to note that this object must reference a type.
Example
Since the instance method already exists, you can simply add the reference to Main.
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void instanceMethodReference(Person person) {
System.out.println("Instance method reference: " + person.name);
}
}
class Main {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("Susan"),
new Person("Kenny")
);
// Reference to a method on an object
persons.sort(String::compareToIgnoreCase);
}
}
As you can see, the method compareToIgnoreCase that resides inside the String class is applied to the persons list. The result will put the persons list into lexicographical order.
4. Constructor Method Reference
This is the last type of method reference.
Definition
This method reference will reference the constructor of a class.
(args) -> new ClassName(args)
This can be transformed into a method reference:
ClassName::new
Unlike the previous method references, with the constructor, the args will not be passed through automatically. Therefore, the Function or BiFunction interface is typically joined with the reference to apply() the arguments to the reference.
Example
class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
class Main {
public static void main(String[] args) {
// Reference to a constructor
Function<String, Person> personConstructor = Person::new;
Person person1 = personConstructor.apply("Susan");
}
This time, a personConstructor variable of type Function<String, Person> contains a method reference to Person as its value. This means that personConstructor will accept a String argument and return a Person.
When the person1 is created, it calls personContructor and uses the apply() function that comes with the Function class to pass through the argument to the constructor method reference (since this type of method reference does not do it automatically).
Summary: Java Method Reference Sheet Example
- There are four types of method references
- Each method reference has a different syntax
- Not all method references function in the same way
Static Method
- Syntax:
Class::staticMethod - Arguments are automatically passed through the reference
Instance Method with a Particular Object
- Syntax:
Class::instanceMethod - Arguments are automatically passed through the reference
Instance Method with an Arbitrary Object and Particular Type
- Syntax:
obj::instanceMethod - Arguments are automatically passed through the reference
Static Method
- Syntax:
Class::new - Arguments are not automatically passed through the reference
- The
FunctionorBiFunctionclasses are used to pass through arguments - The
apply()method is used to pass through the argument to the constructor