0Pricing
Java Academy · Lesson

Static Method References

Replace lambdas that call static methods with ClassName::methodName references.

Static Method References is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Method References?

A method reference is a shorthand lambda that delegates to an existing method. Where a lambda would just call a single method, a method reference is cleaner and more readable.

import java.util.*;
import java.util.stream.*;

List<String> names = List.of("Alice", "Bob", "Carol");

// Lambda:
names.forEach(s -> System.out.println(s));

// Equivalent method reference:
names.forEach(System.out::println);

Static Method Reference Syntax

A static method reference has the form ClassName::staticMethodName. It can be used wherever a functional interface expects a method with the same signature.

import java.util.stream.*;

// Integer.parseInt(String) matches Function<String, Integer>
List<String> strs = List.of("1","2","3","4","5");
List<Integer> nums = strs.stream()
    .map(Integer::parseInt)  // replaces s -> Integer.parseInt(s)
    .collect(Collectors.toList());
System.out.println(nums); // [1, 2, 3, 4, 5]

Math.abs as Method Reference

Static methods from utility classes like Math can be referenced directly:

import java.util.*;
import java.util.stream.*;

List<Integer> values = List.of(-3, 5, -1, 8, -4);
List<Integer> abs = values.stream()
    .map(Math::abs)  // replaces v -> Math.abs(v)
    .collect(Collectors.toList());
System.out.println(abs); // [3, 5, 1, 8, 4]

Static Method Reference with Predicate

Static methods matching a boolean return can serve as Predicate:

import java.util.stream.*;

// String.isEmpty() is an instance method, but we can use a static helper
// Objects.isNull matches Predicate<Object>
List<String> mixed = Arrays.asList("hello", null, "world", null);
long nullCount = mixed.stream()
    .filter(Objects::isNull)
    .count();
System.out.println(nullCount); // 2

long nonNull = mixed.stream()
    .filter(Objects::nonNull)
    .count();
System.out.println(nonNull); // 2

Comparator Static Reference

Static methods that match Comparator can also be referenced:

import java.util.*;
import java.util.stream.*;

List<String> words = List.of("banana", "apple", "cherry");

// String.CASE_INSENSITIVE_ORDER is a Comparator
List<String> sorted = words.stream()
    .sorted(String::compareToIgnoreCase)
    .collect(Collectors.toList());
System.out.println(sorted); // [apple, banana, cherry]

Custom Static Helper Method

Define a static utility method and reference it directly:

class Validator {
    static boolean isPositive(int n) { return n > 0; }
    static String toLabel(int n) { return n > 0 ? "POS" : "NEG"; }
}

List<Integer> nums = List.of(-2, 3, -1, 5, 0);

List<Integer> positive = nums.stream()
    .filter(Validator::isPositive)
    .collect(Collectors.toList());
System.out.println(positive); // [3, 5]

List<String> labels = nums.stream()
    .map(Validator::toLabel)
    .collect(Collectors.toList());

BiFunction with Static Method

Static methods taking two parameters can match BiFunction<T,U,R>:

import java.util.function.*;

// Math.max(int,int) → BiFunction... but needs IntBinaryOperator
java.util.function.IntBinaryOperator maxOp = Math::max;
System.out.println(maxOp.applyAsInt(7, 3)); // 7

// String.format as BiFunction? Only works if signature matches:
// Integer.sum(int,int) → IntBinaryOperator
java.util.function.IntBinaryOperator sum = Integer::sum;
System.out.println(sum.applyAsInt(10, 20)); // 30

Sorting with Comparator.comparing + static ref

Combine Comparator.comparing with method references for clean sorting:

record Person(String name, int age) {}
List<Person> people = List.of(
    new Person("Charlie",30), new Person("Alice",25), new Person("Bob",35)
);

people.stream()
    .sorted(Comparator.comparing(Person::name))
    .forEach(p -> System.out.println(p.name()));
// Alice, Bob, Charlie

When to Use Static Method References

Use static method references when:

  • The lambda body is a single static method call
  • No transformation of arguments is needed
  • The method signature matches the functional interface

If you need to transform arguments or add logic, stick with a lambda.

Readability Comparison

Side-by-side comparison of lambda vs method reference styles:

// Lambda style
Stream.of("1","2","3").map(s -> Integer.parseInt(s));
Stream.of(1,-2,3).filter(n -> n > 0);
Stream.of(-5,2,-1).map(n -> Math.abs(n));

// Method reference style
Stream.of("1","2","3").map(Integer::parseInt);
Stream.of(1,-2,3).filter(Validator::isPositive);
Stream.of(-5,2,-1).map(Math::abs);

Common Pitfall: Wrong Signature

A method reference only works if its signature exactly matches the functional interface's abstract method. If argument types or count differ, use a lambda wrapper instead.

// Math.pow(double, double) needs two args — can't use as Function<Double, Double>
// This won't compile:
// Function<Double, Double> square = Math::pow; // ERROR

// Fix with lambda:
Function<Double, Double> square = n -> Math.pow(n, 2);
System.out.println(square.apply(5.0)); // 25.0

Quick Check

Which of the following is a valid static method reference that works as a Predicate<Object>?

Recap: Static Method References

Key takeaways:

  • Syntax: ClassName::staticMethodName
  • Replaces lambdas that delegate to a single static method
  • The method signature must match the functional interface's abstract method
  • Works with Predicate, Function, Consumer, Comparator, etc.
  • Use lambdas when argument transformation or additional logic is needed

Frequently asked questions

Is the “Static Method References” lesson free?

Yes — the full text of “Static Method References” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Static Method References”?

Replace lambdas that call static methods with ClassName::methodName references. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Static Method References” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Static Method References
  2. Instance Method References on a Specific Instance
  3. Arbitrary-Instance Method References
  4. Constructor References
← Back to Java Academy