Instance Method References on Specific Objects
You'll learn: How to reference an instance method on a particular object using the :: syntax, creating a shorthand for lambda expressions that call methods on specific object instances.
The Core Idea
When you have a specific object already created, and you want to call one of its instance methods repeatedly, you can use the syntax objectReference::instanceMethod. This creates a method reference bound to that particular object instance.
Think of it like giving someone a shortcut button that always calls the same person's phone number—the person (object) is already determined, and you're just triggering their specific method.
Plain English Explanation
Instead of writing a lambda like x -> myString.concat(x) where myString is an existing object, you can write myString::concat. The method reference remembers which object it belongs to and calls that object's method whenever invoked.
This differs from static method references because you're referencing an instance method on a particular, already-existing object, not on the class itself.
Syntax Pattern
// Lambda calling instance method on specific object
Function<String, Boolean> startsWithHello =
s -> "Hello".startsWith(s);
// Equivalent method reference on specific object
Function<String, Boolean> startsWithHello =
"Hello"::startsWith;
Example
String greeting = "Welcome";
Predicate<String> startsWithWel = greeting::startsWith;
boolean result = startsWithWel.test("Wel"); // true
Here, greeting is the specific String object, and we're referencing its startsWith method.
Key Takeaway: Instance method references on specific objects use objectReference::instanceMethod syntax, binding the method reference to a particular object instance you've already created, making your code more concise than equivalent lambda expressions.