Nearby lessons

92 of 125

Java - Method References (::)

📌 What You Will Learn
  • Lambda expressions — the big change
  • Functional interfaces
  • Default and static methods in interfaces
  • The four workhorses: Predicate, Function, Consumer, Supplier
  • Method references (::)
  • The Stream API for processing collections
  • Optional — safe null handling
  • The new Date and Time API

Method References (::) is a core concept of the Java language. This lesson explains Method References (::) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Method References (::)

A method reference is a shorthand for a lambda when you are just calling an existing method. The :: symbol does it:

TypeSyntaxExample
Static methodClass::methodMath::max
Instance methodobject::methodSystem.out::println
Instance of any objectClass::methodString::toUpperCase
ConstructorClass::newStudent::new
Example01
JCode Cell
1names.forEach(name -> System.out.println(name)); // lambda
2names.forEach(System.out::println); // method reference
📝 Key Takeaways
  • Lambda = a function without a name; (params) -> logic.
  • Functional interface = one abstract method, so a lambda can implement it.
  • Interfaces now have default and static methods with bodies.
  • Predicate (test), Function (apply), Consumer (accept), Supplier (get) are the four core functional interfaces.
  • Method references (::) are shorthand for lambdas.
  • Streams process collections with filter/map/sorted/collect pipelines.
  • Optional prevents NullPointerException with orElse/isPresent.
  • java.time (LocalDate, LocalDateTime) replaces the old Date/Calendar cleanly.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1