Skip to main content

Lesson 2 of 8

Method References

Method references are a shorthand for lambdas that delegate to an existing method. They're even more concise than lambdas and read like prose: System.out::println, String::length, Book::new. There are four kinds.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Four Kinds of Method References

Four Kinds of Method References
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class MethodRefs {
    public static void main(String[] args) {
        // 1. Static method reference: ClassName::staticMethod
        Function<String, Integer> parseInt = Integer::parseInt;   // s -> Integer.parseInt(s)
        Function<Double, Double>  abs      = Math::abs;
        System.out.println(parseInt.apply("42"));     // 42

        // 2. Instance method on a specific instance: instance::method
        String prefix = "Book: ";
        Function<String, String> addPrefix = prefix::concat;  // s -> prefix.concat(s)
        Consumer<String> print = System.out::println;         // s -> System.out.println(s)
        print.accept("Hello");

        // 3. Instance method on arbitrary instance of a type: ClassName::instanceMethod
        // Lambda: (String s) -> s.toUpperCase()
        Function<String, String>   upper   = String::toUpperCase;
        Function<String, Integer>  getLen  = String::length;    // s -> s.length()
        Predicate<String>          isBlank = String::isBlank;

        // Key: first parameter becomes 'this', remaining become method args
        BiFunction<String, String, Boolean> startsWith = String::startsWith;
        // (s, prefix) -> s.startsWith(prefix)
        System.out.println(startsWith.apply("Hello", "He")); // true

        // 4. Constructor reference: ClassName::new
        Supplier<ArrayList<String>> listMaker = ArrayList::new;  // () -> new ArrayList<>()
        Function<String, StringBuilder> sbMaker = StringBuilder::new; // s -> new StringBuilder(s)

        // In practice - most common use: stream operations
        List<String> titles = List.of("Clean Code", "Effective Java", "DDIA");
        titles.stream()
              .map(String::toUpperCase)       // type-3 ref
              .sorted()
              .forEach(System.out::println);  // type-2 ref (instance: System.out)
    }
}

Quick Quiz

Tip

Tip

Practice Method References in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Method References

Practice Task

Note

Practice Task - (1) Write a working example of Method References from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Method References is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/8 done