Java Pass Method as Parameter
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java does not pass methods as standalone values in the same way some functional languages do. What it does pass are objects that represent behavior, most commonly lambdas, method references, or instances of functional interfaces. Once you understand that distinction, "passing a method as a parameter" becomes a normal part of Java API design.
Use standard functional interfaces when possible
Since Java 8, the most common approach is to accept a functional interface from java.util.function. For example, if your code needs "something that takes a string and returns a string," Function<String, String> is a natural fit.
The method applyTwice is not receiving a raw method pointer. It is receiving an object whose single abstract method can be invoked. That is what makes lambdas and method references work in Java.
Method references are the closest thing to passing a method
If you already have an existing method whose signature matches the functional interface, you can pass it with a method reference. This is usually the cleanest syntax.
MethodReferenceExample::printValue is the Java syntax that most closely resembles "pass this method." Under the hood, Java still adapts it to the target functional interface type, in this case Consumer<String>.
Create a custom interface for domain-specific behavior
Standard interfaces cover many common shapes, but sometimes a named interface makes the code clearer. That is useful when the behavior has business meaning beyond input and output types.
The @FunctionalInterface annotation is optional but helpful. It tells both readers and the compiler that the interface is intended to have exactly one abstract method.
Anonymous classes still matter in older code
Before Java 8, the usual pattern was to pass an anonymous class. You still see this in older codebases and some callback-heavy APIs.
This is more verbose than a lambda, but it follows the same core rule: Java passes an object that implements a known interface, not a free-floating method.
Choose the right interface shape
The main design question is usually not "how do I pass a method?" but "what behavior does this API need?" If the caller only consumes a value, Consumer<T> is a good fit. If it returns a value, Function<T, R> or Supplier<T> may fit better. If it takes two inputs, BiFunction<T, U, R> or BiConsumer<T, U> may be more natural.
Choosing the right type makes the call site readable and prevents awkward wrappers later.
Common Pitfalls
The biggest pitfall is thinking Java methods are first-class values. They are not. Java supports behavior passing through functional interfaces and object adaptation.
Another common mistake is picking the wrong functional interface. For example, using Runnable when the operation actually needs input data forces you to capture external state and makes the code harder to test.
Developers also run into confusion with overloaded methods. A method reference must match the target functional interface clearly, or the compiler may reject it as ambiguous.
Finally, avoid creating a custom interface when a standard one such as Function, Predicate, or Consumer already expresses the intent well.
Summary
- Java does not pass bare methods directly; it passes behavior through functional interfaces.
- Lambdas and method references are the modern way to supply that behavior.
- Method references are ideal when an existing method already matches the required signature.
- Custom functional interfaces are useful when the behavior has domain meaning.
- Anonymous classes are the older equivalent and still appear in pre-Java 8 code.
Related reading
- Java Path vs File
- Java Producer/Consumer kafka client properties required when accessing a SSL-Auth secured Kafka brokers/cluster?
- Java program that calls external program behaving asynchronously?
- Java project in Eclipse The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- Java properly closing sockets for multi threaded servers
- Java recommended solution for deep cloning/copying an instance
- Java recursive Fibonacci sequence
- Java Reflection How to get the name of a variable?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.