Q58: What are Method References and what are the four syntax variants?

Method References are shorthand lambda syntaxes (Class::method).

  1. Static Method: ContainingClass::staticMethodName (Math::max)
  2. Instance Method of Particular Object: containingObject::instanceMethodName (System.out::println)
  3. Instance Method of Arbitrary Object of Type: ContainingType::methodName (String::toUpperCase)
  4. Constructor Reference: ClassName::new (ArrayList::new)
Key Takeaways
  • Method references provide clean syntax shortcuts for lambda methods.
Knowledge Check

Which method reference syntax corresponds to the lambda expression (String s) -> s.toLowerCase()?