Java Method References

Imtiaz Ahmad
2 min readMay 16, 2024

--

Method referencing in Java is a shorthand syntax that allows you to refer to a method without executing it, particularly useful in the context of functional interfaces. It provides a way to pass methods as arguments to other methods or to create instances of functional interfaces.

Types of Method References

There are several types of method references in Java:

  1. Static Method Reference:
  • Syntax: ClassName::methodName
  • Example: Integer::parseInt refers to the parseInt method of the Integer class.

2. Instance Method Reference of an Object:

  • Syntax: object::methodName
  • Example: System.out::println refers to the println method of the System.out object.

3. Instance Method Reference of a Class Type:

  • Syntax: ClassName::methodName
  • Example: String::length refers to the length method of the String class.

4. Constructor Reference:

  • Syntax: ClassName::new
  • Example: ArrayList::new refers to the constructor of the ArrayList class.

Using Method References

Method references are typically used in contexts where a functional interface is expected, such as with lambda expressions. They offer a concise alternative to lambda expressions when you simply want to delegate to an existing method.

Example: Static Method Reference

// Lambda expression
Function<String, Integer> parseIntLambda = str -> Integer.parseInt(str);

// Method reference to static method
Function<String, Integer> parseIntReference = Integer::parseInt;

Example: Instance Method Reference

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

// Lambda expression
names.forEach(name -> System.out.println(name));

// Method reference to instance method
names.forEach(System.out::println);

Example: Constructor Reference

// Lambda expression
Supplier<List<String>> listSupplierLambda = () -> new ArrayList<>();

// Constructor reference
Supplier<List<String>> listSupplierReference = ArrayList::new;

Advantages of Method References

  • Readability: Method references make the code more readable and concise, especially when referring to existing methods.
  • Code Reuse: Method references allow you to reuse existing methods without writing additional code, promoting modularity and maintainability.
  • Type Safety: Method references provide type checking at compile time, ensuring that the referenced method matches the expected signature of the functional interface.

Considerations

  • Type Inference: The compiler uses context to infer the type of the method reference. This inference is based on the target type (functional interface) to which the method reference is assigned.
  • Limitations: Method references cannot be used for arbitrary expressions; they are limited to referring to existing methods or constructors.

Overall, method referencing in Java complements lambda expressions and functional interfaces, providing a powerful mechanism for leveraging existing methods within the functional programming paradigm introduced in Java 8 and later versions. It enhances code readability and promotes a more declarative style of programming.

--

--