3.1.3 Method References Explained
Method references in Java SE 11 are a concise way to refer to methods or constructors without invoking them. They are used to replace lambda expressions when the lambda expression simply calls an existing method. Method references can make your code more readable and expressive.
Key Concepts
1. Types of Method References
There are four types of method references:
- Reference to a static method: Used when a lambda expression calls a static method of a class.
- Reference to an instance method of a particular object: Used when a lambda expression calls an instance method of a specific object.
- Reference to an instance method of an arbitrary object of a particular type: Used when a lambda expression calls an instance method of any object of a specific type.
- Reference to a constructor: Used when a lambda expression calls a constructor.
2. Syntax
The syntax for method references is ClassName::methodName
for static methods, objectName::methodName
for instance methods of a particular object, ClassName::methodName
for instance methods of an arbitrary object, and ClassName::new
for constructors.
3. Use Cases
Method references are particularly useful when:
- The lambda expression is simple and only calls a single method.
- You want to make your code more readable by reducing verbosity.
- You need to refer to a method or constructor without invoking it immediately.
Explanation and Examples
1. Reference to a Static Method
Consider the following example:
class MathUtils { static int multiply(int a, int b) { return a * b; } } Function<Integer, Integer> multiplier = MathUtils::multiply; System.out.println(multiplier.apply(3, 4)); // Output: 12
Here, MathUtils::multiply
is a method reference to the static method multiply
in the MathUtils
class.
2. Reference to an Instance Method of a Particular Object
Consider the following example:
class StringUtils { String toUpperCase(String s) { return s.toUpperCase(); } } StringUtils utils = new StringUtils(); Function<String, String> upperCase = utils::toUpperCase; System.out.println(upperCase.apply("hello")); // Output: HELLO
Here, utils::toUpperCase
is a method reference to the instance method toUpperCase
of the utils
object.
3. Reference to an Instance Method of an Arbitrary Object of a Particular Type
Consider the following example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); names.forEach(String::toUpperCase); System.out.println(names); // Output: [ALICE, BOB, CHARLIE]
Here, String::toUpperCase
is a method reference to the instance method toUpperCase
of any String
object.
4. Reference to a Constructor
Consider the following example:
class Person { String name; Person(String name) { this.name = name; } } Supplier<Person> personSupplier = Person::new; Person person = personSupplier.get(); System.out.println(person.name); // Output: null
Here, Person::new
is a method reference to the constructor of the Person
class.
Examples and Analogies
Think of method references as shortcuts for lambda expressions. Just as a shortcut on your desktop allows you to quickly access a program without navigating through folders, method references allow you to quickly refer to a method or constructor without writing out the full lambda expression.
By mastering method references, you can make your Java SE 11 code more concise and readable, improving both your productivity and the clarity of your code.