Instance Method References on a Specific Instance
Capture a specific object and reference one of its instance methods for callbacks and listeners.
Instance Method References on a Specific Instance is a free Java Academy lesson on CoddyKit — lesson 2 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.
Specific Instance Reference
An instance method reference on a specific instance captures a particular object and references one of its methods. Syntax: instance::methodName.
// System.out is a specific PrintStream instance
List<String> names = List.of("Alice","Bob","Carol");
names.forEach(System.out::println); // System.out is the captured instanceCapturing a Custom Object Instance
Any object can be captured. The method reference borrows the instance at the time of creation:
class Greeter {
private String prefix;
Greeter(String prefix) { this.prefix = prefix; }
String greet(String name) { return prefix + ", " + name + "!"; }
}
Greeter g = new Greeter("Hello");
java.util.function.Function<String, String> greetFn = g::greet;
System.out.println(greetFn.apply("Alice")); // Hello, Alice!
System.out.println(greetFn.apply("Bob")); // Hello, Bob!Using as Supplier
A no-arg instance method bound to a specific object matches Supplier<T>:
StringBuilder sb = new StringBuilder("Hello World");
java.util.function.Supplier<String> toString = sb::toString;
System.out.println(toString.get()); // Hello World
sb.append(" !");
System.out.println(toString.get()); // Hello World ! (sb is live)Using as Predicate
Boolean-returning instance methods match Predicate when bound to a specific instance:
String prefix = "Java";
java.util.function.Predicate<String> startsWith = prefix::equals;
// Hmm, this checks if prefix.equals(candidate).
// More useful:
Predicate<String> startsWithJ = s -> s.startsWith("J");
// But a specific instance Predicate:
StringBuilder pattern = new StringBuilder("hello");
Predicate<String> equalsHello = ((CharSequence)"hello")::equals;
System.out.println(equalsHello.test("hello")); // true
System.out.println(equalsHello.test("world")); // falseListener / Callback Pattern
Specific-instance method references are ideal for event listeners and callbacks — they naturally capture the handler object:
class EventHandler {
void onEvent(String event) {
System.out.println("Handling: " + event);
}
}
EventHandler handler = new EventHandler();
List<String> events = List.of("click","hover","submit");
events.forEach(handler::onEvent); // captures the specific handler instancePractical: Logger Bound to a Class
Bind a logger to a specific logger instance for use as a Consumer:
import java.util.logging.*;
Logger log = Logger.getLogger("MyApp");
List<String> messages = List.of("Starting","Processing","Done");
messages.stream()
.map(String::toUpperCase)
.forEach(log::info); // log is the specific Logger instanceReference Captures State at Binding Time
The object state visible through the reference reflects the object's live state, not a snapshot from when the reference was created:
List<String> list = new ArrayList<>(List.of("A","B"));
java.util.function.Supplier<Integer> sizeRef = list::size;
System.out.println(sizeRef.get()); // 2
list.add("C");
System.out.println(sizeRef.get()); // 3 (reflects live state)Comparing Specific vs Arbitrary Instance
Two distinct method reference forms look similar — understand the difference:
// Specific instance: s is captured at binding
String s = "hello";
Predicate<String> p1 = s::equalsIgnoreCase; // String::equalsIgnoreCase bound to 'hello'
// Arbitrary instance: receiver is the stream element
Function<String, String> p2 = String::toUpperCase; // receiver IS the elementUse Case: Thread-safe method delegation
Bind a synchronized method on a service object as a functional interface for safe async use:
class Counter {
private int count = 0;
synchronized void increment(String ignored) { count++; }
int get() { return count; }
}
Counter c = new Counter();
List<String> tasks = List.of("t1","t2","t3");
tasks.forEach(c::increment);
System.out.println(c.get()); // 3When the Captured Instance Is Null
If the captured object is null when the method reference is created, a NullPointerException is thrown at invocation time, not at binding time:
StringBuilder sb = null;
// Reference is created without error:
java.util.function.Supplier<String> ref = sb::toString;
try {
ref.get(); // NPE thrown here
} catch (NullPointerException e) {
System.out.println("NPE at invocation");
}Comparison Summary
Three method reference forms compared:
- ClassName::staticMethod — static, no receiver
- instance::instanceMethod — bound to specific object
- ClassName::instanceMethod — receiver is the stream element (arbitrary-instance)
Quick Check
Given EventHandler h = new EventHandler(); list.forEach(h::handle); — what is the receiver of the handle call for each element?
Recap: Specific Instance Method References
Key takeaways:
- Syntax: instance::methodName — captures a specific object
- The captured object is the receiver for every invocation
- Ideal for callbacks, listeners, and logging patterns
- The captured instance's live state is visible through the reference
- NPE at invocation if instance is null (not at binding)
Frequently asked questions
Is the “Instance Method References on a Specific Instance” lesson free?
Yes — the full text of “Instance Method References on a Specific Instance” 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 “Instance Method References on a Specific Instance”?
Capture a specific object and reference one of its instance methods for callbacks and listeners. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Instance Method References on a Specific Instance” 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
- Static Method References
- Instance Method References on a Specific Instance
- Arbitrary-Instance Method References
- Constructor References